请您检查以下代码是否存在'条件表达式中的数据类型不匹配' 异常的错误?我似乎找不到问题的根源......
可空
DateTime?
类型的 *record.Date
显式转换为 DateTime
*record.Date
被设置为可为空以用于程序中的其他用途。但是为 INSERT 操作 设置的 record.Date
是从 DateTimePicker 中检索的,因此 record.Date
值对于此方法,永远不应为空。
哪里
AND(如果你想知道的话)
从我的 Access 文件(设计 View ):
谢谢!
这是 AddRecord 方法。谢谢!
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();
}
}
最佳答案
所以...[问题已解决] :D
来自 HERE我了解到
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.
这意味着方便的 AddWithValue
对我来说很快...
感谢@LarsTech 和@DJKraze 在演示文稿困惑的情况下帮助我!
关于C# Access OleDb 标准表达式中的数据类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751424/