简体   繁体   English

将 DateTime 值作为参数传递给 OleDbCommand

[英]Passing a DateTime value as a parameter to an OleDbCommand

I have a problem passing a DateTime value to a query as a DbParameter.我在将 DateTime 值作为 DbParameter 传递给查询时遇到问题。 It seems the time part of the DateTime value gets stripped away.似乎 DateTime 值的时间部分被剥离了。

Here is a sample code in C#:这是 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.我的问题是 Date 参数似乎没有通过它的时间部分到达 Access 并且 SomeDateField 总是有 00:00:00 作为时间。

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 OleDbType 没有 DateTime 枚举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如果使用 DBType.DateTime,“参数的类型特定于 .NET Framework 数据提供者。指定类型会将 Parameter 的值转换为数据提供者的类型,然后再将值传递给数据源。如果类型不是指定时,ADO.NET 从参数 object 的 Value 属性推断参数的数据提供者类型。“http://msdn.microsoft.com/en-us/library/system.x

Make sure your data type for SomeDateField is DateTime and not Date .确保SomeDateField的数据类型是DateTime而不是Date Also, try to make另外,尝试制作

p.DbType = DbType.DateTime;

When you'd use OleDbType.DateTime instead of DbType.Date , I'm sure it will work.当您使用OleDbType.DateTime而不是DbType.Date时,我相信它会起作用。

But, as I understand from your post, you do not want to be so specific, and use the more general DbType enum (and classes)?但是,正如我从您的帖子中了解到的那样,您不想如此具体,而是使用更通用的 DbType 枚举(和类)?

However, I think you should use the more specific OleDb classes in your data-access layer.但是,我认为您应该在数据访问层中使用更具体的 OleDb 类。 Using the less specified 'DbType' classes, is kind of useless, because, when you're targetting another database-type, chances are quite big that your SQL-syntax will have to change as well, since each DBMS uses its own dialect (sometimes only minor changes, but still... ).使用较少指定的“DbType”类是无用的,因为当您针对另一种数据库类型时,您的 SQL 语法也必须更改的可能性很大,因为每个 DBMS 都使用自己的方言(有时只有微小的变化,但仍然......)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM