5

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

在此处输入图像描述

*record.Date可空DateTime?类型显式转换为DateTime

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

在哪里

在此处输入图像描述

和(如果你想知道)

在此处输入图像描述


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

在此处输入图像描述


谢谢!


这是 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();
        }
    }
4

3 回答 3

14

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

这里我了解到

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

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

意思是方便AddWithValue的人给我拉了一个快速的……

感谢@LarsTech 和@DJKraze 在演示混乱的情况下帮助我!

于 2014-09-09T20:18:12.223 回答
0
OleDbConnection connection = LABMeetingRecordsDB.GetConnection();
string insertStatement = "INSERT INTO DocumentInfo " + "([FileName], [Date], [Subject], [Type]) " + "VALUES (?, ?, ?, ?)";
OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection);
insertCommand.CommandType = CommandType.Text;
insertCommand.Parameters.AddWithValue("@FileName", record.FileName);
insertCommand.Parameters.AddWithValue("@Date", (DateTime)record.Date ?? (object)DBNull.Value);
insertCommand.Parameters.AddWithValue("@Subject", record.Subject);
insertCommand.Parameters.AddWithValue("@Type", record.getDBType ?? (object)DBNull.Value);
connection.Open();
try
{
  insertCommand.ExecuteNonQuery();
}
catch(OleDbException e)
{
   LogYourMessage(e.Message);
}

将 DBNull.Value 转换为(对象)DBNull.Value;是处理对象的正确方法试试这个我刚刚检查过并做了一个类似的测试。

于 2014-09-09T18:43:58.783 回答
0

我认为您也可以使用 ToString() 使您的日期数据为正确的格式,该格式将被访问接受

 insertCommand.Parameters.AddWithValue("@Date", record.Date.ToString("dd-MM-yy"));
于 2015-02-20T14:00:52.467 回答