PC Review


Reply
Thread Tools Rate Thread

DbType.DateTime not equivalent to OleDbType.Date

 
 
Roy Soltoff
Guest
Posts: n/a
 
      15th Apr 2009
Using Framework 3.5: I've converted a data base access project that was
written against the OleDbProvider to now be provider independent using a
DbProviderFactory. In doing so, I had to migrate my update and insert
commands from OleDbCommand to DbCommand created using the factory. This
meant that I changed the Parameter items to use DbType rather than
OleDbType.

The program uses a Vb Date type that is equivalent to a System.DateTime. I
previously the OleDbType.Date type for updating date columns; the database
is an Access Jet 4.0 database. So I used the DbType.DateTime type in the
provider independent code. This updates the database date columns okay only
when the date/time value is no more precise than minutes (i.e. #4/15/2009
11:45:00 AM#). If the date/time value has a precision of seconds (i.e.
#4/15/2009 11:45:25 AM#), the update fails with a "Data type mismatch in
criteria expression" error. It acts as if the mapping of DbType.DateTime
does not have the precision needed for the database date column where, in
fact, the OleDbProvider using OleDbType.Date was fine. There is a
DbType.DateTime2 that appears to reflect the same precision of a System.Date
however that type has no mapping to the OldeDbType (that was the error
message).

Does anyone know if there is a bug in the framework's conversion of
DbType.DateTime to the OleDb provider?


 
Reply With Quote
 
 
 
 
Lingzhi Sun [MSFT]
Guest
Posts: n/a
 
      16th Apr 2009
Hi Roy,

Thank you for your report. This problem, "OleDbParameter with
DbType.DateTime throws 'Data type mismatch in criteria expression'", has
been confirmed as a product issue by the corresponding product unit. For
detail, please see
http://connect.microsoft.com/VisualS...k.aspx?Feedbac
kID=94377.

In your post, you have mentioned that you are currently using
DbProviderFactory to do data access. To make your application work
correctly, you can refer to the following two recommended workarounds.

====================
1. Use OleDbType.Date instead of DbType.DateTime when we encounter an
OleDbParameter

For detail, please see this code snippet:
===================
// OleDb connection string templete
string ConnectionTemplate = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source={0};Persist Security Info=False;";
// OleDb data provider
string DbProvider = "System.Data.OleDb";
try
{
// Data source path
string path = Path.GetFullPath("...\\DB.mdb");
// Create the connection string
string connectionString = string.Format(ConnectionTemplate, path);

// Create the DbProviderFactory
DbProviderFactory df = DbProviderFactories.GetFactory(DbProvider);
// Create the corresponding DbConnection
DbConnection conn = df.CreateConnection();
conn.ConnectionString = connectionString;
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO TestTable (Inserted) VALUES (?)";

// Create the corresponding DbDataParameter
IDbDataParameter pInserted = cmd.CreateParameter();
// Try to convert the DbDataParameter to OleDbParameter
OleDbParameter p = pInserted as OleDbParameter;
// If the convert fails, use DbType.DateTime
if (null == p)
pInserted.DbType = DbType.DateTime;
// Else set the OleDbType.Date
else
p.OleDbType = OleDbType.Date;
// Set the DateTime.Now value
pInserted.Value = DateTime.Now;
cmd.Parameters.Add(pInserted);

// Insert the record
conn.Open();
int recordsAffected = cmd.ExecuteNonQuery();
conn.Close();

Console.WriteLine("{0} records affected", recordsAffected);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
===================

2. Insert DateTime.ToString() to the DbDataParameter's value

For detail, please see this code snippet:
===================
...
IDbDataParameter pInserted = cmd.CreateParameter();
pInserted.DbType = DbType.DateTime;
// Set the DateTime.Now.ToString() to the
// DbDataParameter's value
pInserted.Value = DateTime.Now.ToString();
cmd.Parameters.Add(pInserted);
...
===================

If you have any further questions related to this case, please be free to
post here.

Have a nice day!

Regards,
Lingzhi Sun (v-(E-Mail Removed), remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HelpUpdateing table in C# using an OleDbType.Date parm in where cl =?Utf-8?B?UGF0cmljaw==?= Microsoft Access 2 23rd Apr 2007 05:44 AM
DbType vs. OleDbType vs. SqlDbType Tom Winter Microsoft ADO .NET 2 13th Apr 2006 04:49 AM
Which OleDbType is correct for DateTime field in Access 2000 database? sincethe2003 Microsoft ASP .NET 1 21st Jul 2004 05:10 AM
DateTime to OleDbType (MS Access) Flynn Arrowstarr Microsoft ADO .NET 1 14th Oct 2003 01:42 PM
ADO.Net Convert Data\Time (MS Access) to OleDbType.Date JHatfield Microsoft ADO .NET 0 10th Sep 2003 06:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:05 AM.