条件表达式中的数据类型不匹配 |访问、OleDb、C# [英] Data type mismatch in criteria expression | Access, OleDb, C#
问题描述
我使用 C# 从 MS Access 读取/更新数据.我的代码是:
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);
}
}
说到日期,我遇到了麻烦.这会引发条件表达式中的数据类型不匹配".错误.(我删除了 WHERE 子句以使其更简单)我应该附上 [LastLogin]= 吗?带单引号的问号,# 符号 .. 没有帮助.任何有关如何使用 Access 和 OleDb 提供程序处理 DateTime 对象的线索将不胜感激.
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.
提前致谢.
推荐答案
OleDb 和日期存在一个已知问题.尝试执行以下操作:
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")
这篇关于条件表达式中的数据类型不匹配 |访问、OleDb、C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!