简体   繁体   English

在MS Access Insert.Into期间解决“条件表达式中的数据类型不匹配。”

[英]Troubleshooting “Data type mismatch in criteria expression.” during MS Access Insert.Into

I'm creating a basic customer inventory application, and when converting the code from using SQL Server to using MS Access (which I'm quite a bit less versed in), I ran into a "Data type mismatch" error when trying to do a basic insert. 我正在创建一个基本的客户清单应用程序,将代码从使用SQL Server转换为使用MS Access(我不太了解)时,在尝试执行此操作时遇到“数据类型不匹配”错误基本插入。

I've looked into several similar questions here, and double checked the msdn syntax guide, but I can't find a reason why the script I've written would generate that error. 我在这里研究了几个类似的问题,并仔细检查了msdn语法指南,但找不到我编写的脚本会产生该错误的原因。 I changed my code several times to try and ensure proper data type (ending up with what I have below with explicit typing and adding the value later). 我多次更改代码以尝试确保正确的数据类型(最后通过显式键入并在后面添加值来完成下面的内容)。 I've actually even taken the string and pasted it into MS Access (sans white space and double quotes), and it seems to work just fine with the values given. 实际上,我什至将字符串带入并粘贴到MS Access(没有空格和双引号)中,并且似乎可以很好地使用给定的值。 At this point, I'm really and truly stumped, and I'm wondering if it might just be a quirk with the Oledb adapter? 在这一点上,我确实感到非常困惑,我想知道它是否可能是Oledb适配器的怪癖? Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

// SQL query defined elsewhere:
public static readonly string sqlAddCustomerNotes = "INSERT INTO CustomerNotes (Customer_ID, Notes, NotesDate) "
            + "VALUES(@Customer_ID, @Notes, @NotesDate);";
// end sql query

// data access function
public static void addNotes(int customerID, string notes, DateTime notesDate)
        {
            string query = Scripts.sqlAddCustomerNotes;
            using (
                OleDbCommand dbCommand = new OleDbCommand()
                {
                    Connection = new OleDbConnection(ConnectionAccess.connString),
                    CommandType = CommandType.Text,
                    CommandText = query,
                    Parameters =
                        {
                            new OleDbParameter("@Customer_ID", OleDbType.Integer),
                            new OleDbParameter("@Notes", OleDbType.LongVarChar),
                            new OleDbParameter("@NotesDate", OleDbType.DBTimeStamp)
                        }
                }) // end using parenthetical
            { // begin using scope
                dbCommand.Parameters[0].Value = customerID;
                dbCommand.Parameters[1].Value = notes;
                dbCommand.Parameters[2].Value = notesDate;
                foreach (OleDbParameter param in dbCommand.Parameters)
                { // replace ambiguous null values with explicit DBNulls.
                    if (param.Value == null)
                    {
                        param.Value = DBNull.Value;
                    }
                }
                dbCommand.Connection.Open();
                int rowsAffected = dbCommand.ExecuteNonQuery();
                dbCommand.Connection.Close();
                Console.WriteLine($"Rows affected: {rowsAffected}");
            }
        } // end addCustomerNotes
/*
table "CustomerNotes" has the following columns:datatypes
    CustomerNotes_ID: AutoNumber
    Customer_ID: Number
    Notes: Memo
    NotesDate: Date/Time
    CreateDate: Date/Time

test case (in code) was:
    @Customer_ID = 5
    @Notes = "customer might change last name to simpson."
    @NotesDate = {6/26/2019 12:05:39 PM}
*/

它可能是一个日期 ,而不是一个时间戳

new OleDbParameter("@NotesDate", OleDbType.DBDate)

Considering June7's comment about delimiters, it seems the issue lies in some issue inherent to the OleDbParameter type. 考虑到June7关于定界符的评论,似乎问题出在OleDbParameter类型固有的某个问题上。 In SQL Server terms, I do want DateTime (not Date ), but representing it as a DBTimeStamp seems to make it unrecognizable by Access. 用SQL Server的术语来说,我确实希望使用DateTime (而不是Date ),但是将其表示为DBTimeStamp似乎会使Access无法识别它。

For the time being, I've sent the date as a VarChar and allowed Access to convert it however its internal engine sees fit. 目前,我已将日期作为VarChar发送,并允许Access对其进行转换,但是其内部引擎认为合适。 It feels/seems wrong, but it does, in fact, solve the problem. 感觉/似乎是错误的,但实际上可以解决问题。

Parameters =
                        {
                            new OleDbParameter("@Customer_ID", OleDbType.Integer),
                            new OleDbParameter("@Notes", OleDbType.LongVarChar),
                            new OleDbParameter("@NotesDate", OleDbType.VarChar)
                        }

EDIT : Just saw June7's latest comment, and there was in fact, an answer in another thread . 编辑 :刚刚看到June7的最新评论,实际上, 另一个线程中有一个答案。 OleDbType.DBDate doesn't do what I want, but OleDbType.Date does. OleDbType.DBDate不能满足我的要求,但是OleDbType.Date做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM