简体   繁体   English

条件表达式中的数据类型不匹配| Access,OleDb,C#

[英]Data type mismatch in criteria expression | Access, OleDb, C#

I read/update data from MS Access using C#. 我使用C#从MS Access读取/更新数据。 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]=? (为了保持简单,我删除了WHERE子句)我是否考虑将[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. 任何有关如何使用Access和OleDb提供程序处理DateTime对象的线索将不胜感激。

Thanks in advance. 提前致谢。

There is a known issue with OleDb and dates. OleDb和日期存在已知问题。 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")

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;

Firstly, no your SQL statement should be: 首先,你的SQL语句不应该是:

"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. 作为您进入LastLogin字段的日期时间而不是实际的logintime参数。

maybe try 也许试试

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

instead, pass it as String (and maybe enclose with # then) 相反,将它作为String传递(并可能用#括起来)

Should it not be 不应该

"UPDATE Customers SET LastLogin='@LastLogin'"

And @LastLogin should be @LastLogin应该是

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") + "'"

Try setting the "DBTYPE" property of the parameter to identify it as a date, datetime or datetime2 as appropriate... 尝试设置参数的“DBTYPE”属性,以便将其标识为日期,日期时间或日期时间2 ......

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

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

相关问题 C#访问OleDb条件表达式中的数据类型不匹配 - C# Access OleDb Data type mismatch in criteria expression oledb异常未在标准表达式中的c#数据类型不匹配中处理 - oledb exception was unhandled in c# Data type mismatch in criteria expression 条件表达式Oledb Access数据库中的数据类型不匹配 - Data type mismatch in criteria expression Oledb Access database System.Data.OleDb.OleDbException:“标准表达式中的数据类型不匹配。” C# 错误 - System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.' C# error System.Data.OleDb.OleDbException:'条件表达式中的数据类型不匹配。 在C#中 - System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression. in c# 使用 Oledb、C#、Dapper 的条件表达式中的数据不匹配 - data mismatch in criteria expression using Oledb, C#, Dapper 条件表达式MS-Access C#中的数据类型不匹配 - Data type mismatch in criteria expression ms-access c# C#AND ACCESS - 条件表达式中的数据类型不匹配 - C# AND ACCESS - Data type mismatch in criteria expression “条件表达式中的数据类型不匹配。” ACCESS 2010和C# - “Data type mismatch in criteria expression.” ACCESS 2010 and C# 使用MS Access的C#中的条件表达式中的数据类型不匹配? - Data type mismatch in criteria expression in C# with MS Access?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM