我在将 DateTime 值作为 DbParameter 传递给查询时遇到问题。看来 DateTime 值的时间部分被剥夺了。
下面是 C# 中的示例代码:
DbProviderFactory _factory = OleDbFactory.Instance;
DbCommand cmd = _factory.CreateCommand();
cmd.CommandText = "INSERT INTO SomeTable (SomeDateField) VALUES (?)";
DbParameter p = _factory.CreateParameter();
p.ParameterName = ""; // Not necessary
p.Value = DateTime.Now; // assume Time != 00:00:00
p.DbType = DbType.Date; // DateTime and DateTime2 don't work
cmd.Parameters.Add(p);
我的问题是,Date 参数似乎无法通过其时间部分到达 Access,而 SomeDateField 的时间始终为 00:00:00。
我不想做类似的事情:
cmd.CommandText = "INSERT INTO SomeTable (SomeDateField) VALUES (#" + aDateTimeString + "#)";
确保您的数据类型SomeDateField
is DateTime
并不是Date
。另外,尝试使
p.DbType = DbType.DateTime;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)