I am building an application where when i press an button the action need to be save in a Microsoft Access Database, but when i press the button i get the error "Data type mismatch in criteria expression" from the next part of code
string dbconnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"data source =ElevatorLog.accdb";
string dbcommand = "insert into [Elevator] ([MovingAction],[TimeAction]) values (@movingaction, @timeaction)";
string timeaction = DateTime.Now.ToShortDateString();
listBox1.Items.Add(movingaction + "\t\t" + timeaction + "\t\t");
OleDbConnection conn_db = new OleDbConnection(dbconnection);
OleDbCommand comm_insert = new OleDbCommand(dbcommand, conn_db);
OleDbDataAdapter adapter_insert = new OleDbDataAdapter(comm_insert);
comm_insert.Parameters.AddWithValue("@timeaction", timeaction);
comm_insert.Parameters.AddWithValue("@movingaction", movingaction);
conn_db.Open();
comm_insert.ExecuteNonQuery();
conn_db.Close();
I don't know what your datatypes are but guessing partially from code:
string dbconnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"data source =ElevatorLog.accdb";
string dbcommand = @"insert into [Elevator]
([MovingAction],[TimeAction])
values
(@movingaction, @timeaction)";
//? listBox1.Items.Add(movingaction + "\t\t" + timeaction + "\t\t");
using (OleDbConnection conn_db = new OleDbConnection(dbconnection))
using (OleDbCommand comm_insert = new OleDbCommand(dbcommand, conn_db))
{
comm_insert.Parameters.Add("@movingaction", OleDbType.DBDate).Value = movingaction; // not sure if this is DateTime
comm_insert.Parameters.Add("@timeaction", OleDbType.DBDate).Value = DateTime.Now;
conn_db.Open();
comm_insert.ExecuteNonQuery();
conn_db.Close();
}
PS: Just a suggestion, choose a better database like postgreSQL, MS SQL, MySQL, SQLite...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.