简体   繁体   English

查询访问数据库时条件表达式中的数据类型不匹配

[英]Data type mismatch in criteria expression when Query Access DB

I'm having an issue inserting a row into my access db. 我在将行插入到Access数据库时遇到问题。 I keep getting a "Data type mismatch in criteria expression". 我不断收到“条件表达式中的数据类型不匹配”。 I've tried tons of different formatted queries and can't seem to figure out where I'm going wrong. 我尝试了很多不同格式的查询,但似乎无法弄清楚我要去哪里。

    public void addUserToDB(string username)
    {
        string updateStr = "Insert Into Users ([Username], [Currency], [Joined], [Online], [Notes]) "
                         + "Values ( ?, '0', ?, 'Yes', '')";
        OleDbCommand update = new OleDbCommand(updateStr, con);
        update.Parameters.Add("", OleDbType.VarChar).Value = username;
        update.Parameters.AddWithValue("","'#" + DateTime.Now.ToString("G") + "#'");
        update.Parameters.AddWithValue("","'#" + DateTime.Now.ToString("G") + "#'");
        execute(update);
    }

It's not my connection string or anything else since all my other queries work just fine. 这不是我的连接字符串或其他任何东西,因为我的所有其他查询都能正常工作。 It has to be something in here. 这里一定有东西。 I'm assuming is may have something to due with the date time. 我假设可能与日期时间有关。

Access DB: 访问数据库:
Username: ShortText 用户名:ShortText
Currency: Number 货币:数字
Joined: Date/Time in "General Date" Format 已加入:“常规日期”格式的日期/时间
Online: Yes/No 在线:是/否
Notes: ShortText 注意:ShortText

Since your Currency column is Number and Online seems Yes/No ( It stores 1 bit) , you don't need to use single quotes with them. 由于您的“ Currency列为“ Number并且“ Online似乎Yes/No它存储1位) ,因此您不需要在它们之间使用单引号。 Using single quotes threat them as a character. 使用单引号会威胁它们作为字符。

string updateStr = "Insert Into Users ([Username], [Currency], [Joined], [Online], [Notes]) "
                     + "Values ( ?, 0, ?, Yes, '')";
                                   ^^^    ^^^

And your Joined is DateTime , you shouldn't try to insert string representation of your DateTime.Now . 并且JoinedDateTime ,则不应尝试插入DateTime.Now字符串表示形式。 Just insert it as a DateTime as ODBC canonical date format. 只需将其作为DateTime插入为ODBC规范日期格式即可。

From documentation ; 文件 ;

Date values must be either delimited according to the ODBC canonical date format or delimited by the datetime delimiter ("#"). 日期值必须根据ODBC规范日期格式来定界,或者由日期时间定界符(“#”)来定界。 Otherwise, Microsoft Access will treat the value as an arithmetic expression and will not raise a warning or error. 否则,Microsoft Access会将值视为算术表达式,并且不会引发警告或错误。

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

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