gpt4 book ai didi

c# - 查询 Access DB 时条件表达式中的数据类型不匹配

转载 作者:搜寻专家 更新时间:2023-10-30 23:48:16 24 4
gpt4 key购买 nike

我在向我的 Access 数据库中插入一行时遇到问题。我一直收到“标准表达式中的数据类型不匹配”。我已经尝试了大量不同格式的查询,但似乎无法弄清楚哪里出了问题。

    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);
}

这不是我的连接字符串或其他任何东西,因为我的所有其他查询都工作正常。它必须是这里的东西。我假设可能与日期时间有关。

Access 数据库:
用户名:短文本
货币:数字
加入:“常规日期”格式的日期/时间
在线:是/否
备注:短文本

最佳答案

由于您的Currency 列是Number 并且Online 似乎是Yes/No(它存储 1 bit),您不需要对它们使用单​​引号。使用单引号威胁他们作为一个字符。

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

而您的JoinedDateTime,您不应尝试插入DateTime.Now 的字符串表示形式。只需将它作为 DateTime 作为 ODBC 规范日期格式插入即可。

来自 documentation ;

Date values must be either delimited according to the ODBC canonical date format or delimited by the datetime delimiter ("#"). Otherwise, Microsoft Access will treat the value as an arithmetic expression and will not raise a warning or error.

关于c# - 查询 Access DB 时条件表达式中的数据类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24950297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com