V
Vincent
Hi !
I'm trying to insert a datetime into a Access Database and
I've a "Data type mismatch in criteria expression"
Exception.
I need to store the Date AND the Time. ( using
System.Data.DbType.Date works but do not store the Time)
I've written a very cimple program that error :
Any help greathly apreciated, Thanks !
Vincent
// Start Exmaple
DateTime insertedDate = DateTime.Now;
string databaseName = "../../somedb.mdb";
using(IDbConnection connection = new OleDbConnection
(String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source={0};",databaseName)))
{
try
{
connection.Open();
// Cleanup Table
IDbCommand deleteCmd = connection.CreateCommand();
deleteCmd.CommandText ="DELETE * FROM sometable";
deleteCmd.ExecuteNonQuery();
// Insert date
IDbCommand cmd = connection.CreateCommand();
cmd.CommandText ="INSERT INTO sometable
(somedatefield) VALUES(?)";
IDbDataParameter parameter= cmd.CreateParameter();
parameter.DbType = System.Data.DbType.DateTime;
parameter.Value = insertedDate;
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
I'm trying to insert a datetime into a Access Database and
I've a "Data type mismatch in criteria expression"
Exception.
I need to store the Date AND the Time. ( using
System.Data.DbType.Date works but do not store the Time)
I've written a very cimple program that error :
Any help greathly apreciated, Thanks !
Vincent
// Start Exmaple
DateTime insertedDate = DateTime.Now;
string databaseName = "../../somedb.mdb";
using(IDbConnection connection = new OleDbConnection
(String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source={0};",databaseName)))
{
try
{
connection.Open();
// Cleanup Table
IDbCommand deleteCmd = connection.CreateCommand();
deleteCmd.CommandText ="DELETE * FROM sometable";
deleteCmd.ExecuteNonQuery();
// Insert date
IDbCommand cmd = connection.CreateCommand();
cmd.CommandText ="INSERT INTO sometable
(somedatefield) VALUES(?)";
IDbDataParameter parameter= cmd.CreateParameter();
parameter.DbType = System.Data.DbType.DateTime;
parameter.Value = insertedDate;
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}