标准表达式中的数据类型不匹配 |访问、OleDb、C#

发布于 2024-08-08 04:32:02 字数 596 浏览 3 评论 0原文

我使用 C# 从 MS Access 读取/更新数据。 我的代码是:

public static void UpdateLastLogin(int userid, DateTime logintime) ///logintime = DateTime.Now
{
    string sql = @"UPDATE [Customers] SET [LastLogin]=?";
    OleDbParameter[] prms = new OleDbParameter[] { 
     new OleDbParameter("@LastLogin",logintime)
    };
    using (DAL dal = new DAL())
    {
        dal.UpdateRow(sql, false, prms);
    }
}

当谈到日期时,我遇到了麻烦。 这会引发“条件表达式中的数据类型不匹配”。错误。 (我删除了 WHERE 子句以使其更简单) 我应该附上 [LastLogin]= 吗?带单引号的问号、# 符号 .. 没有帮助。 任何有关如何使用 Access 和 OleDb 提供程序处理 DateTime 对象的线索将不胜感激。

提前致谢。

I read/update data from MS Access using C#.
My code is:

public static void UpdateLastLogin(int userid, DateTime logintime) ///logintime = DateTime.Now
{
    string sql = @"UPDATE [Customers] SET [LastLogin]=?";
    OleDbParameter[] prms = new OleDbParameter[] { 
     new OleDbParameter("@LastLogin",logintime)
    };
    using (DAL dal = new DAL())
    {
        dal.UpdateRow(sql, false, prms);
    }
}

When it comes to Dates, I having trouble.
This throws a "Data type mismatch in criteria expression." error.
(I removed WHERE clause for keeping it simpler)
Am I suuposed to enclose [LastLogin]=? question mark with single quotes, # signs .. does not help.
Any leads on how to handle DateTime objects with Access and OleDb provider will be greatly appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请持续率性 2024-08-15 04:32:02

OleDb 和日期存在一个已知问题。尝试执行以下操作:

OleDbParameter p = parameter as OleDbParameter;
if (null == p)
  parameter.DbType = DbType.DateTime;
else
  p.OleDbType = OleDbType.Date;

或使用显式格式字符串:

value.ToString("yyyy-MM-dd hh:mm:ss")

There is a known issue with OleDb and dates. Try doing something like:

OleDbParameter p = parameter as OleDbParameter;
if (null == p)
  parameter.DbType = DbType.DateTime;
else
  p.OleDbType = OleDbType.Date;

Or use explicit format string:

value.ToString("yyyy-MM-dd hh:mm:ss")
旧夏天 2024-08-15 04:32:02

我使用以下代码解决了这个问题

OleDbCommand cmd = new OleDbCommand(qry, cnn);
cmd.Parameters.Add("datenow", OleDbType.Date);
cmd.Parameters["datenow"].Value = DateTime.Now;

I solved this using the following code

OleDbCommand cmd = new OleDbCommand(qry, cnn);
cmd.Parameters.Add("datenow", OleDbType.Date);
cmd.Parameters["datenow"].Value = DateTime.Now;
伴随着你 2024-08-15 04:32:02

首先,您的 SQL 语句不应该是:

"UPDATE Customers SET LastLogin=@LastLogin"

其次,您收到日期不匹配错误的原因可能是您传递的“?”作为您的日期时间输入 LastLogin 字段,而不是实际的登录时间参数。

Firstly, no your SQL statement should be:

"UPDATE Customers SET LastLogin=@LastLogin"

Secondly, the reason you are receiving the date mismatch error will probably be your passing '?' as your date time into the LastLogin field instead of the actual logintime parameter.

停顿的约定 2024-08-15 04:32:02

也许可以尝试

DateTime.Now.ToShortDateString() + ' ' + DateTime.Now.ToShortTimeString()

将其作为字符串传递(然后可能用 # 括起来)

maybe try

DateTime.Now.ToShortDateString() + ' ' + DateTime.Now.ToShortTimeString()

instead, pass it as String (and maybe enclose with # then)

你的呼吸 2024-08-15 04:32:02

不应该吗

"UPDATE Customers SET LastLogin='@LastLogin'"

@LastLogin 应该是

logintime.ToString("yyyy-MM-dd hh:mm:ss")

编辑
你不能直接内联整个事情吗?

"UPDATE Customers SET LastLogin='" + logintime.ToString("yyyy-MM-dd hh:mm:ss") + "'"

Should it not be

"UPDATE Customers SET LastLogin='@LastLogin'"

And @LastLogin should be

logintime.ToString("yyyy-MM-dd hh:mm:ss")

edit
Could you not just inline the whole thing?

"UPDATE Customers SET LastLogin='" + logintime.ToString("yyyy-MM-dd hh:mm:ss") + "'"
请止步禁区 2024-08-15 04:32:02

尝试设置参数的“DBTYPE”属性以将其标识为适当的日期、日期时间或日期时间2...

prms[0].DbType = DbType.DateTime;

新的 OleDbParameter() 调用有 7 个签名,因此您可以更改签名实例,或者只是显式执行我在上面进行了采样,因为在这种情况下您只有 1 个参数。

Try setting the "DBTYPE" property of the parameter to identify it as a date, datetime or datetime2 as appropriate...

prms[0].DbType = DbType.DateTime;

There are 7 signatures to the new OleDbParameter() call, so you may change the signature instance, or just do explicitly as I sampled above since you only had 1 parameter in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文