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.