【发布时间】:2013-03-13 09:39:37
【问题描述】:
此搜索代码是否具有如下任何日期格式?捕捉日期格式“dd-MM-yyyy”。 我的桌子是:
id | dat | user | remain
-----------------
1 | 01-03-2013 | x | 1000
----------------
2 | 01-03-2013 | x | 1200
----------------
3 | 02-03-2013 | y | 1100
----------------
4 | 02-03-2013 | y | 1300
----------------
5 | 03-03-2013 | z | 1200
----------------
6 | 03-03-2013 | z | 1400
-------------------------
private void textBox10_Leave(object sender, EventArgs e)
{
if ((textBox10.Text == "yyyy/MM/dd") || (textBox10.Text == "dd/MM/yyyy") || (textBox10.Text == "yyyy-MM-dd"))
{
textBox10.Text = "dd-MM-yyyy";
using (SqlConnection cn = new SqlConnection(Class1.x))
{
cn.Open();
string cm = "select id from item_treasury where dat='" + textBox10.Text + "' order by id";
using (SqlCommand cmd = new SqlCommand(cm, cn))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read() == true)
{
if (dr.HasRows == false)
{
MessageBox.Show("xxxxxxxxxxxxxxxxxxxx");
this.Close();
}
comboBox1.Items.Add(dr[0].ToString());
}
}
}
}
}
}
我想更正这段代码。 请帮助我!
【问题讨论】:
-
不,这是不对的。这很容易出现SQL injection,最糟糕的那种......使用准备好的语句!
-
这也不会传递日期本身,它会传递格式。你想在这里做什么以日期格式传递? textBox10 也不是一个有用的名称,comboBox1 也不是您应该更改这些名称。
-
为什么任何输入都不像“dd-MM-yyyy”这样的文本搜索程序没有响应?
-
您应该使用参数并将日期作为 date 传递给 SQL 服务器,而不是作为一些任意格式的字符串。而且,希望在服务器端,数据存储在类型为
datetime(或更现代的变体之一)的列中。将日期存储或处理为字符串是(与日期相关的)错误的最大来源之一。 -
如果我将 02/03/2013 输入到 textBox10.Text 结果是 (3) 和 (4) 到 comboBox1.Items,如果我输入的文本不是之前的状态,程序会给我一个消息。