传递一个DateTime值作为参数传递给一个OleDbCommand [英] Passing a DateTime value as a parameter to an OleDbCommand

查看:419
本文介绍了传递一个DateTime值作为参数传递给一个OleDbCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个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 =; //不必要
p.Value = DateTime.Now; !//假设时间为00:00:00
p.DbType = DbType.Date; // DateTime和DATETIME2不起作用

cmd.Parameters.Add(对);



我的问题是日期参数,似乎并没有达到访问与它的一部分时间和SomeDateField总是有。00:00:00随着时间



我不想做这样的事情:

  cmd.CommandText =INSERT INTO SomeTable(SomeDateField)VALUES(#+ aDateTimeString +#); 


解决方案

OleDbType没有一个DateTime enumeraton
http://msdn.microsoft.com/en-us /library/system.data.oledb.oledbtype.aspx



如果使用DBType.DateTime的类型的参数是特定于.NET Framework数据提供程序,指定类型的值传递给数据源之前的参数数据提供类型的值转换,如果不指定类型,ADO.NET从Value属性推断参数的数据类型提供Parameter对象
的http:// MSDN。 microsoft.com/en-us/library/system.data.dbtype.aspx


I have a problem passing a DateTime value to a query as a DbParameter. It seems the time part of the DateTime value gets stripped away.

Here is a sample code in 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);

My problem is that the Date parameter does not seem to reach Access with it's time part and SomeDateField always has 00:00:00 as time.

I don't want to do something like:

cmd.CommandText = "INSERT INTO SomeTable (SomeDateField) VALUES (#" + aDateTimeString + "#)";

解决方案

OleDbType does not have a DateTime enumeraton http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtype.aspx

If you use DBType.DateTime, "The type of a parameter is specific to the .NET Framework data provider. Specifying the type converts the value of the Parameter to the data provider Type before passing the value to the data source. If the type is not specified, ADO.NET infers the data provider Type of the Parameter from the Value property of the Parameter object. " http://msdn.microsoft.com/en-us/library/system.data.dbtype.aspx

这篇关于传递一个DateTime值作为参数传递给一个OleDbCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆