关于在sql语句中使用.Net DateTime有几个问题,但没有一个能解决我的问题.
我正在使用以下代码查询Oracle数据库:@H_404_3@
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));