关于在sql语句中使用.Net DateTime有几个问题,但没有一个能解决我的问题.
private DataTable QueryByIdAndDate(string id,DateTime fdate,DateTime tdate) { string query = "SELECT * FROM table WHERE ID = :id AND DATE_TIME BETWEEN :from AND :to" DbCommand cmd = db.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = query; DbParameter fromDate = CreateDateParameter(cmd,fdate); fromDate.ParameterName = "from"; DbParameter toDate = CreateDateParameter(cmd,tdate); toDate.ParameterName = "to"; DbParameter idParam = CreateStringParameter(cmd,id); idParam.ParameterName = "id"; cmd.Parameters.AddRange(new DbParameter[] { fromDate,toDate,idParam }); return db.ExecuteQuery(cmd); } private DbParameter CreateDateParameter(DbCommand cmd,DateTime date) { DbParameter param = cmd.CreateParameter(); param.DbType = DbType.DateTime; param.Direction = ParameterDirection.Input; param.Value = date; return param; }
但它不能正常工作.当像这样尝试代码时:
DataTable result = QueryByIdAndDate("12345",DateTime.Now,DateTime.Now.AddDays(1));
它给出以下错误:
ORA-01847:每月的日期必须介于1和1的最后一天之间