作为this question的后续,当我使用该代码并且其中一个结果对DateTime列具有NULL值时,我收到以下错误:
错误[22007] [缓存ODBC] [状态:22007] [本地代码22007] [E:!work \ projects \ dotnet_feed \ dotnetFeed]日期时间格式无效
这是完整的代码,改编自上面的问题:
public void doQuery(string id, string sql, string ds)
{
string connectionString = "DSN=" + ds + ";";
string queryString = sql;
payload result = new payload();
var resultList = new List<Dictionary<string, dynamic>>();
result.id = id;
result.timestmap = DateTime.Now;
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
OdbcCommand command = new OdbcCommand(queryString, connection);
try
{
connection.Open();
OdbcDataReader reader = command.ExecuteReader();
if (reader.HasRows) //not working when no results.
{
while (reader.Read())
{
var t = new Dictionary<string, dynamic>();
for (var i = 0; i < reader.FieldCount; i++)
{
t[reader.GetName(i)] = reader[i];
}
resultList.Add(t);
}
}
else
{
log("NO RESULTS");
}
reader.Close();
result.data = resultList;
string output = JsonConvert.SerializeObject(result);
}
catch (Exception ex)
{
log(ex.Message);
}
}
}
同样,如果结果集在datetime
列中没有空值,则可以正常工作。它也适用于其他列中的空值。我该如何解决这个问题?
ex.ToString()
&#34; System.Data.Odbc.OdbcException(0x80131937):ERROR [22007] [缓存 ODBC] [状态:22007] [本机代码22007] \ r \ n [E:\!work \ projects \ dotnet_feed \ dotnetFeed \] \ r \ n无效 日期时间格式\ r \ n at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)\ r \ n at System.Data.Odbc.OdbcDataReader.GetData(Int32 i,SQL_C sqlctype,Int32 cb,Int32&amp; cbLengthOrIndicator)\ r \ n at System.Data.Odbc.OdbcDataReader.GetData(Int32 i,SQL_C sqlctype)\ r \ n
at System.Data.Odbc.OdbcDataReader.internalGetDateTime(Int32 i)\ r \ n
在System.Data.Odbc.OdbcDataReader.GetValue(Int32 i,TypeMap typemap)\ r \ n在System.Data.Odbc.OdbcDataReader.GetValue(Int32) i)\ r \ n在System.Data.Odbc.OdbcDataReader.get_Item(Int32 i)\ r \ n at E:\!work \ projects \ dotnet_feed \ dotnetFeed \ dotnet_feed \ Form1.cs中的dotnet_feed.Form1.doQuery(queryObject qo):line 92&#34;串
第92行: t[reader.GetName(i)] = reader[i];