求教在vs2010中应该如何写对于access数据库中的sql条件查询语句
先上代码:
【c#】
string sql = "SELECT 'stuName','stuId','stuTime' FORM Student (WHERE stuTime BETWEEN \'" + startTime + "\' AND \'" + endTime + "\')";
startTime是string类型的开始时间格式为:2013/3/14
endTime是结束时间
我在vs2010中调试了很多遍就是不对,请各位大大帮忙解决!万分感谢!
------解决方案--------------------access 日期比较两边不是用单引号,要用#
string sql = "SELECT 'stuName','stuId','stuTime' FORM Student (WHERE stuTime BETWEEN #" + startTime + "# AND #" + endTime + "#)";
------解决方案--------------------用Pamameters:
DateTime theDate = new DateTime(2012,10,16);
var cmd = new OleDbCommand();
cmd.CommandText = "INSERT INTO sometable (column) VALUES (@p_bar)";
cmd.Parameters.Add ("@p_bar", OleDbType.DateTime).Value = theDate;
代码来自:
http://stackoverflow.com/questions/12922292/insert-datetime-into-access
------解决方案--------------------SELECT 'stuName','stuId','stuTime' FORM Student WHERE stuTime BETWEEN CONVERT(varchar(10),"+startTime+", 23) AND CONVERT(varchar(10), "+endTime+", 23)";
自己在调试下吧,吧时间类型转换下,最好使用参数。
------解决方案--------------------WHERE datediff('d','"+startime+"',stuTime)>0 and datediff('d','"+endtime+"',stuTime)<0