Inserting DateTime value
-
Hi! I have a problem when I try to insert DateTime value in an access database. I receive this exception: "Syntax error in INSERT INTO statement." I try two different ways to add Datetime and no one works!
newDataRow["Timestamp"] = DateTime.Now.ToString();
andnewDataRow["Timestamp"] = DateTime.Now;
But if I remove all DateTime stuff in my database and my code, I am able to insert data whitout any problem. Here my code:DataSet dataSet = new DataSet(); string dbGenConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="; string dbConn = dbGenConn + "fileName.mdb" + "; Persist Security Info=False;"; OleDbConnection conn = new OleDbConnection(dbConn); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM Data", conn)); OleDbCommandBuilder dataCmdBuilder = new OleDbCommandBuilder(adapter); adapter.Fill(dataSet, "Data"); for(int i = 0; i < items.Length; i++) { DataRow newDataRow = dataSet.Tables["Data"].NewRow(); newDataRow["DataID"] = i; newDataRow["Timestamp"] = DateTime.Now.ToString(); newDataRow["Quality"] = 192 dataSet.Tables["Data"].Rows.Add(newDataRow); } adapter.Update(dataSet, "Data"); conn.Close();
-
Hi! I have a problem when I try to insert DateTime value in an access database. I receive this exception: "Syntax error in INSERT INTO statement." I try two different ways to add Datetime and no one works!
newDataRow["Timestamp"] = DateTime.Now.ToString();
andnewDataRow["Timestamp"] = DateTime.Now;
But if I remove all DateTime stuff in my database and my code, I am able to insert data whitout any problem. Here my code:DataSet dataSet = new DataSet(); string dbGenConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="; string dbConn = dbGenConn + "fileName.mdb" + "; Persist Security Info=False;"; OleDbConnection conn = new OleDbConnection(dbConn); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM Data", conn)); OleDbCommandBuilder dataCmdBuilder = new OleDbCommandBuilder(adapter); adapter.Fill(dataSet, "Data"); for(int i = 0; i < items.Length; i++) { DataRow newDataRow = dataSet.Tables["Data"].NewRow(); newDataRow["DataID"] = i; newDataRow["Timestamp"] = DateTime.Now.ToString(); newDataRow["Quality"] = 192 dataSet.Tables["Data"].Rows.Add(newDataRow); } adapter.Update(dataSet, "Data"); conn.Close();
i think this is the problem: newDataRow["Timestamp"] = DateTime.Now.ToString(); if your database field is a text field use newDataRow["Timestamp"] = Convert.ToString(DateTime.Now); and if it's a datetime (much better) use it this way newDataRow["Timestamp"] = DateTime.Now;