我想在c#中使用此查询
从ms访问数据库中选择数据da = new OleDbDataAdapter(
"SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] between #"
+ dateTimePicker2.Value.ToString()
+ "# AND #"
+ dateTimePicker3.Value.ToString()
+ "#",
VCON);
它返回null 请帮帮我。
答案 0 :(得分:0)
您必须格式化日期,如:
dateTimePicker2.Value.ToString("yyyy/MM/dd")
你也可以这样做:
da = new OleDbDataAdapter(
"SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] between "
+ dateTimePicker2.Value.ToString("#yyyy/MM/dd#")
+ " AND "
+ dateTimePicker3.Value.ToString("#yyyy/MM/dd#") ,
VCON);
但是,我建议你使用参数来避免SQL注入,如下所示:
string query = "SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] " +
"BETWEEN @startDate AND @endDate";
OleDbCommand cmd = new OleDbCommand(query, VCON);
cmd.Parameters.AddWithValue("@startDate ", DbType.DateTime).Value =
dateTimePicker1.Value.ToString("yyyy/MM/dd");
cmd.Parameters.AddWithValue("@endDate ", DbType.DateTime).Value =
dateTimePicker2.Value.ToString("yyyy/MM/dd");
da = new OleDbDataAdapter(cmd);