C# 来自usercontrol的两个日期之间的数据

C# 来自usercontrol的两个日期之间的数据,c#,C#,在我的表单中,我创建了一个label textbox datepicker的用户控件 在这个表单中,我放置了一个数据网格,当我单击submit按钮时,我需要两个日期之间的数据。我的代码是: private void btnSubmit_Click(object sender, EventArgs e) { SqlConnection cs = new SqlConnection("Data Source=IRIS-CSG-174;Initi

在我的表单中,我创建了一个label textbox datepicker的用户控件 在这个表单中,我放置了一个数据网格,当我单击submit按钮时,我需要两个日期之间的数据。我的代码是:

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'" +userControl11.Text+ "'" + "and'" + userControl12.Text + "'", cs);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                da.Fill(ds, "lib_issue_details");
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.DataBindings.Add(new Binding("text", ds, "lib_issue_details"));

                cs.Close();


            }

但这段代码并没有从数据库中提供数据,它只提供数据库列名称,因为我从datepicker中选择了两个日期,因为我希望两个日期之间的数据。

首先,这将修复sql注入,其次,它将删除时间限制,您不必创建绑定,所以我删除了该行并添加了注释

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 CAST(book_issue_on AS Date) between CAST(@StartDate AS Date) and CAST(@EndDate AS Date)", cs);
cmd.Parameters.Add(new SqlParameter("@StartDate", dateTimePicker1.Text)); //If this is a datetimepicker it would be better to use: dateTimePicker1.Value
cmd.Parameters.Add(new SqlParameter("@EndDate", dateTimePicker1.Text)); //If this is a datetimepicker it would be better to use: dateTimePicker1.Value

SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataTable dt = new DataTable(); DataSet ds = new DataSet(); 
da.Fill(ds, "lib_issue_details"); 
dataGridView1.DataSource = ds.Tables[0]; 

//By Default it will infer the columns from your dataset, otherwise create the columns on your designer
//dataGridView1.DataBindings.Add(new Binding("text", ds, "lib_issue_details"));
cs.Close();

我对日期类型也有类似的问题。如果在SQL Manager中运行SELECT语句,它是否会将数据返回给您?可能数据只是空的。。添加断点并查看
ds.Tables[0].Rows.Count的值是多少-可能是0。首先,您需要修复该SQL注入漏洞。从安全性和性能角度来看,字符串连接查询都是错误的。第二,实际生成的查询是什么?当您在数据库上手动运行它时,它返回什么?