我有一个ODBC数据源,其中一列是DateTime。如果我尝试对字段(IsDbNull
,GetValue
,GetValues
)执行任何操作,如果该值等于null,则会得到异常。
System.ArgumentOutOfRangeException occurred
HResult=-2146233086
Message=Year, Month, and Day parameters describe an un-representable DateTime.
Source=mscorlib
StackTrace:
at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
at System.DateTime..ctor(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second)
at System.Data.ProviderBase.DbBuffer.ReadDateTime(Int32 offset)
at System.Data.Odbc.CNativeBuffer.MarshalToManaged(Int32 offset, SQL_C sqlctype, Int32 cb)
at System.Data.Odbc.OdbcDataReader.internalGetDateTime(Int32 i)
at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i, TypeMap typemap)
at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)
at System.Data.Odbc.OdbcDataReader.IsDBNull(Int32 i)
at Sandbox_Form.Form1.button1_Click(Object sender, EventArgs e) in e:\Code\Sandbox Form\Form1.cs:line 49
InnerException:
如果我将查询更改为select cast(First_Visit as varchar) as First_Visit_cast from Clients
,则会返回文本字符串0/0/0 12:00:00
作为结果。
以下是我的测试代码的片段。前17行工作正常,但是第18行具有“null”值的日期并抛出显示的异常。
const string Query = @"select cast(First_Visit as varchar) as First_Visit_cast
,First_Visit
from Clients";
private void button1_Click(object sender, EventArgs e)
{
using (var con = new OdbcConnection("Dsn=My_ODBC_DSN"))
{
con.Open();
using (var cmd = new OdbcCommand(Query, con))
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
var test = rdr.GetValue(0); //returns "0/0/0 12:00:00" as string.
var test3 = rdr.IsDBNull(1); //Throws an ArgumentOutOfRangeException: "Year, Month, and Day parameters describe an un-representable DateTime."
var test2 = rdr.GetValue(1); //Throws an ArgumentOutOfRangeException: "Year, Month, and Day parameters describe an un-representable DateTime."
var records = new object[rdr.FieldCount];
rdr.GetValues(records); //Throws an ArgumentOutOfRangeException: "Year, Month, and Day parameters describe an un-representable DateTime."
}
}
}
}
有什么方法可以解决这个问题吗?我真的想把我的查询写成Select * from TableName
,而不是识别所有的DateTimes并明确地将它们作为字符串转换。
答案 0 :(得分:1)
DateTime的最小值为Jan 1 0001。您的值小于该值,因此在解析期间会出现异常。
您的值不为null,但它也不是有效的.Net DateTime。
您可以在db中将此空值写入此字段,而不是“0/0/0 12:00:00”
如果您无法更改数据,则可以在您身边找到解决方法:
object MyGetValue(int i)
{
if(rdr.GetFieldType(i) == typof(DateTime))
{
try{
return rdr.GetValue(i);
}
catch(ArgumentOutOfRangeException)
{
return null;
}
}
return rdr.GetValue(i);
}