【发布时间】:2015-06-10 03:06:33
【问题描述】:
我想在 textbox1 和 textbox2 中输入日期值,但我认为我不确定字符串 cmd 有人可以帮我检查问题吗?请
try
{
con = new SqlConnection(MyConnectionString);
con.Open();
int tp;
string sqlstr = "select * from viewReC_store where stk_date_time between '" + TextBox1.Text + "' and '" + TextBox2.Text + "'";
cmd = new SqlCommand(sqlstr, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
}
catch (Exception ex)
{
Label4.Text = "ERROR"+ex ;
}
con.Close();
Blockquote string sqlstr = "select * from viewReC_store where stk_date_time between '" + TextBox1.Text + "' and '" + TextBox2.Text + "'";
我猜这行
【问题讨论】:
-
你有什么错误吗?
-
没有任何错误,只是对我不起作用点击 btn
-
尝试返回
sqlstr并在 SQL Server 中运行它以查看是否有任何行。 -
可以在sql server中运行没有任何错误
-
您已使用
text box's来捕获日期,这可能会导致意外结果,除非date与SQL 期望的格式不完全相同。尝试将您的sqlstr更改为此。select * from viewReC_store where stk_date_time between CONVERT(DATETIME, '" + TextBox1.Text + "', 102) and CONVERT(DATETIME, '" + TextBox2.Text + "', 102)"
标签: c# sql-server