将DateTime值作为DbParameter传递给查询时遇到问题。看起来时间部分的DateTime值被剥离了。将DateTime值作为参数传递给OleDbCommand
这里是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);
我的问题是,日期参数似乎没有达到访问与它的一部分时间和SomeDateField总是00:00:00的时间。
我不想做这样的事情:
cmd.CommandText = "INSERT INTO SomeTable (SomeDateField) VALUES (#" + aDateTimeString + "#)";
什么是数据类型数据库表中的列的列?另外你是什么意思'DbType.DateTime'不起作用? –
当我使用DbType.DateTime和DbType.DateTime2时,执行命令时出现'条件表达式中的数据类型不匹配'异常。 –
我使用Access作为后端,因此列数据类型是Date,但它与.NET中的DateTime等效,它也可以包含时间部分。 –