数据表中DATE类型的字段 为空是用GetOracleDateTime ()去读取。用GetDateTime()读取时,会发生异常。
具体分析如下:
............
//链接数据库
OracleDataReader reader = cmd.ExecuteReader();
读取DATE类型的值,赋给一个string 类型字段,
string str=reader.GetOracleDateTime(4).tostring();//4代表是在Oracle数据库表中的第4列
如果数据库表中 有值的话,得到时间格式是 yyyy-mm-dd hh-mm-ss 例如:“2013-08-07 11:34:24”
这种情况就可以直接读出了。但是如果想要只要 日期,而去掉时间
做这样的转换:string str1=DateTime.Parse(str).ToShortDateString()//----------强制转换为DateTime类型的,在选取DateTime类型
//-----------的ToShortDateString()转换属性,就得到仅有日期了
例如:“2013-08-07 11:34:24” 就 转换成了 ”2013-08-07“
难点:如果Oracle中数据表中不含有日期,为空时 其返回值问题:
GetOracleDateTime类型GetOracleDateTime()返回的是NULL
而DateTime类型的GetDateTime()返回的不是NULL,所以为空是要用GetOracleDateTime()读取。