C#访问OleDb条件表达式中的数据类型不匹配

Moo*_*ing 4 c# ms-access oledbexception oledbparameter

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

在此输入图像描述

*record.Date可空DateTime?类型的*被明确地转换为DateTime

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

哪里

在此输入图像描述

和(如果你想知道的话)

在此输入图像描述


从我的Access文件(设计视图):

在此输入图像描述


谢谢!


这是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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Moo*_*ing 13

所以...... [问题已解决]:D

这里我学到了

标准表达式不匹配的问题是由于OleDbType分配给用于表示调用AddWithValue时DateTime.Now值的参数.

AddWithValue选择的OleDbType是DBTimeStamp,但Access需要OleDbType.Date.

这意味着方便AddWithValue拉我一个快...

感谢@LarsTech和@DJKraze帮助我,尽管演示文稿混乱!