C#访问OleDb条件表达式中的数据类型不匹配 [英] C# Access OleDb Data type mismatch in criteria expression
问题描述
你。请查收下面的代码为给我一个数据类型在条件表达式不匹配的错误例外?我似乎无法找到问题...
来源
* record.Date
可空日期时间?
类型是明确铸造到的DateTime
* record.Date
设置程序中的其它用途为空。但 record.Date
为 INSERT操作设置的是从的的DateTimePicker 的,所以这个方法record.Date
值不应该为空。
WHERE
和(如果你想知道)
从我访问文件(设计视图):
感谢您!
这里的方法的AddRecord。谢谢!
公共静态INT的AddRecord(录音记录)
{
OleDbConnection的连接= LABMeetingRecordsDB.GetConnection( );
串insertStatement =INSERT INTO DocumentInfo+
([文件名],[日期],[主题],[类型])+
VALUES(?,?,?, ?);
尝试{
的OleDbCommand的InsertCommand =新的OleDbCommand(insertStatement,连接);
insertCommand.Parameters.AddWithValue(@文件名,record.FileName);
insertCommand.Parameters.AddWithValue(@日,(DATETIME)record.Date);
insertCommand.Parameters.AddWithValue(@主题,record.Subject);
insertCommand.Parameters.AddWithValue(@类型,record.getDBType());
connection.Open();
insertCommand.ExecuteNonQuery();
串selectStatement =SELECT IDENT_CURRENT('DocumentInfo')FROM DocumentInfo
的OleDbCommand的SelectCommand =新的OleDbCommand(selectStatement,连接);
INT的recordId = Convert.ToInt32(selectCommand.ExecuteScalar());
AddCategory(连接,recordId所,record.Category);
返回的recordId;
}赶上(OleDbException前){
罚球前;
} {最后
的Connection.close();
}
}
所以, ... [问题解决了]:D
从的这里我得知
条件表达式中不匹配的问题是由于分配给用来表示当你调用AddWithValue的DateTime.Now
值的参数
OleDbType。
由AddWithValue choosen的OleDbType是DBTIMESTAMP,但Access想
A OleDbType.Date。
块引用>
意思就是方便
AddWithValue
拉我上一快一...
感谢您@LarsTech和@DJKraze帮了我,尽管混乱的演示!
Would you please kindly check the following code for errors that give me a 'Data type mismatch in criteria expression' exception? I just can't seem to find the source of the problem...
*
record.Date
of nullableDateTime?
type is explicitly casted toDateTime
*
record.Date
is set as nullable for other uses in the program. But therecord.Date
set for the INSERT operation is retrieved from a DateTimePicker, so arecord.Date
value for this method should never be null.WHERE
AND (in case you're wondering)
From my Access file (Design View):
Thank you!
Here's the AddRecord method. Thanks!
public static int AddRecord(Record record) { OleDbConnection connection = LABMeetingRecordsDB.GetConnection(); string insertStatement = "INSERT INTO DocumentInfo " + "([FileName], [Date], [Subject], [Type]) " + "VALUES (?, ?, ?, ?)"; try { OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection); insertCommand.Parameters.AddWithValue("@FileName", record.FileName); insertCommand.Parameters.AddWithValue("@Date", (DateTime)record.Date); insertCommand.Parameters.AddWithValue("@Subject", record.Subject); insertCommand.Parameters.AddWithValue("@Type", record.getDBType()); connection.Open(); insertCommand.ExecuteNonQuery(); string selectStatement = "SELECT IDENT_CURRENT('DocumentInfo') FROM DocumentInfo"; OleDbCommand selectCommand = new OleDbCommand(selectStatement, connection); int recordID = Convert.ToInt32(selectCommand.ExecuteScalar()); AddCategory(connection, recordID, record.Category); return recordID; } catch (OleDbException ex) { throw ex; } finally { connection.Close(); } }
解决方案So...[PROBLEM SOLVED] :D
From HERE I learnt that
The problem of the mismatch in criteria expression is due to the OleDbType assigned to the parameter used to represent the DateTime.Now value when you call AddWithValue.
The OleDbType choosen by AddWithValue is DBTimeStamp, but Access wants a OleDbType.Date.
Meaning that the convenient
AddWithValue
pulled a fast one on me...Thank you @LarsTech and @DJKraze for helping me out despite the confusion of the presentation!
这篇关于C#访问OleDb条件表达式中的数据类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!