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

标签 c# ms-access oledbexception oledbparameter

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

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/

相关文章:

c# - 将 double 格式化为纬度/经度人类可读格式

c# - 无法加载文件或程序集或其依赖项。该系统找不到指定的文件

excel - 当我尝试将某些内容变暗为图表或图表对象时出错

excel - Microsoft.ACE.OLEDB.12.0 当前记录集不支持更新尝试更新 Access 时收到的错误

ssis - Jet DB 引擎 - SSIS OLEDBERROR - 来自外部数据库驱动程序的意外错误 (1)

c# - 我可以通过编程方式确定 (OLEDB) 超出了哪个列长度吗?

c# - MVVM 模式中的 Viewpager

c# - 模式匹配变量范围

ms-access - 当用户双击文档时触发按钮私有(private)子

c# - Microsoft.Jet.OLEDB.4.0 提供程序未注册