大家好,当我使用Datetimepicker
ex:wednesday, April 26,2017
插入日期时,我希望在两个日期之间进行搜索,但它不能给我所有日期之间的日期?
插入代码
cmd.CommandText = "insert into [Table1] ( [Employee Name],[Date],[Hours] ) values ('" + textBox1.Text + "','" + dateTimePicker1.Text + "','" + textBox2.Text + "')";
在代码之间搜索:
cmd.CommandText = "select * from Table1 where [Date] between '" + dateTimePicker1.Text + "' AND '" + dateTimePicker2.Text + "' ";
答案 0 :(得分:0)
首先使用参数,因为现在你的代码容易受到sql注入的攻击。所以这样做:
cmd.CommandText = ("insert_questions @store_result, @store_title,
@store_des");
cmd.Parameters.AddWithValue("@store_result", store_result);
并使用dateTimePicker1.Value而不是应该执行此操作的文本。
答案 1 :(得分:0)
使用参数来避免从字符串转换日期值:
cmd.CommandText = "insert into [Table1] ( [Employee Name],[Date],[Hours] ) values (@empName, @date, @hours)";
cmd.Parameters.AddWithValue("@empName", textBox1.Text);
cmd.Parameters.AddWithValue("@date", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@hours", textBox2.Text);
// ...................
cmd.CommandText = "select * from Table1 where [Date] between @startDate AND @endDate ";
cmd.Parameters.AddWithValue("@startDate", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@endDate", dateTimePicker2.Value);
答案 2 :(得分:0)
您正在DateTime
插入string
,并尝试使用字符串作为过滤器来获取数据。
所以,很少有事情:
Date
的类型为Date而不是string。dateTimePicker1.Value
代替Text
,因为Value
会返回DateTime
个对象。总结一下:
cmd.CommandText = "insert into [Table1] ( [Employee Name],[Date],[Hours] ) values (?, ? , ?)";
cmd.Parameters.AddWithValue("EmployeeName", textBox1.Text);
cmd.Parameters.AddWithValue("Date", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("Hours", textBox2.Text);
cmd.CommandText = "select * from Table1 where [Date] between ? AND ?";
cmd.Parameters.AddWithValue("DateFrom", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("DateTo", dateTimePicker2.Value);