将文本框设置为文本框 当在文本框中写入日期时, 两个日期之间的corrs数据来自数据网格 * 我的代码是: *
private void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection cs = new SqlConnection("Data Source=IRIS-CSG-174;Initial Catalog=library_system;Integrated Security=True");
cs.Open();
SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between=" + textBox1.Text+""+"and"+ textBox2.Text +"", cs);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds, "lib_issue_details");
dataGridView1.DataBindings.Add(new Binding("text", ds, "lib_issue_details.book_issue_on"));
它显示“=”附近的语法不正确的错误。 在这段代码中帮助我...提前感谢!!!
答案 0 :(得分:4)
我想这一行
SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between=" + textBox1.Text+""+"and"+ textBox2.Text +"", cs);
应该是这样的:
SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between " + textBox1.Text + " and " + textBox2.Text, cs);
或者,更好的方法是使用parameters。
SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between @date1 and @date2", cs);
cmd.Parameters.Add("@date1", SqlDbType.DateTime);
cmd.Parameters["@date1"].Value = textBox1.Text;
cmd.Parameters.Add("@date2", SqlDbType.DateTime);
cmd.Parameters["@date2"].Value = textBox1.Text;
我可能会考虑使用DateTimePicker而不是文本框。
答案 1 :(得分:3)
从查询中删除“=”
应该where book_issue_on between date1 and date2
不需要'='符号
您的代码应该是
SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between " + textBox1.Text+""+" and "+ textBox2.Text +"", cs);
答案 2 :(得分:1)
使用命令参数。仅用于安全性(SQL Injection)
一些例子: