问题描述
- c# 从oracle中查询数据,设置时间参数问题,在线等
-
各位大神,小弟刚接触c#时间不长,写了个程序,从界面上datatimepicker中取出时间,然后在c#代码的sql语句中设置时间参数,无论怎么设置要不就查不到数据,要不就是时间格式设置的不对,下面是代码
public DataTable QueryUsedMaterial(string classgroup, string beginTime, string endTime)
{string sql_QueryMaterialsQty = @"select rtrim(mh.schemeid) as 订单,it1.name as 牌号,mh.field3 as 班组," +"ud.modelcode as 盘号名称,ud.callqty as 呼料数量,ud.returnqty as 退还数量,ud.usedqty as 使用数量," +"it.name as 物料名称,ud.USEDMATERIALSDETAILID as ID,ud.usedmaterialsid from machineproductplanhis mh " +"inner join usedmaterialshead uh on rtrim(mh.schemeid) = uh.field1 inner join usedmaterialsdetail ud " +"on uh.usedmaterialsid = ud.usedmaterialsid inner join item it on ud.itemcode = it.code inner join item " +"it1 on mh.itemid = it1.itemid where mh.field3 =:classgroup and mh.createdate >= " +"to_date(:beginTime,'yyyy/mm/dd hh24:mi:ss') and mh.createdate <=to_date(:endTime,'yyyy/mm/dd hh24:mi:ss')"; OracleParameter[] paras = { new OracleParameter(":classgroup",OracleType.NVarChar), new OracleParameter(":beginTime",OracleType.DateTime), new OracleParameter(":endTime",OracleType.DateTime) }; paras[0].Value = classgroup.ToString(); paras[1].Value = DateTime.Parse(beginTime); paras[2].Value = DateTime.Parse(endTime); //paras[1].Value = beginTime.ToString(); //paras[2].Value = endTime.ToString(); DataTable dt = OracleHelper.SelectDateTable(sql_QueryMaterialsQty, paras); return dt;
解决方案
可以参考以下例子
public static DataTable Query()
{
const string sSql = "select xh,lsh,name from table where gxsj>to_date(:gxsj,'yyyy-MM-dd HH24:mi:ss')";
OracleParameter[] parameters = {new OracleParameter(":gxsj", OracleType.VarChar, 10)};
parameters[0].Value = DateTime.Now.AddHours(-3).ToString("yyyy-MM-dd HH") + ":00:00";
return DBUtility.DbHelperOra.Query(sSql, parameters).Tables[0];
}
解决方案二:
http://blog.csdn.net/xjzdr/article/details/4253427
关于C#用参数的形式操作Oracle数据库日期字段时的一些问题
时间: 2025-01-27 03:14:26