gpt4 book ai didi

C# Access OleDb 标准表达式中的数据类型不匹配

转载 作者:太空狗 更新时间:2023-10-30 00:07:28 25 4
gpt4 key购买 nike

请您检查以下代码是否存在'条件表达式中的数据类型不匹配' 异常的错误?我似乎找不到问题的根源......

enter image description here

可空 DateTime? 类型的

*record.Date 显式转换为 DateTime

*record.Date 被设置为可为空以用于程序中的其他用途。但是为 INSERT 操作 设置的 record.Date 是从 DateTimePicker 中检索的,因此 record.Date 值对于此方法,永远不应为空

哪里

enter image description here

AND(如果你想知道的话)

enter image description here


从我的 Access 文件(设计 View ):

enter image description here


谢谢!


这是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com