我很難將記錄插入到我的訪問數據庫中。我已經嘗試了訪問中的查詢,它插入正常。我也在查詢生成器中嘗試過查詢,但它也起作用,但是如果我運行此代碼,它聲稱已將該記錄插入到數據庫中,但是當我檢查數據庫時,沒有新記錄的跡象。將記錄插入到Access數據庫中
oleDbCommandCreateAppointment.Parameters["appointmentDate"].Value = "06/04/2012";
oleDbCommandCreateAppointment.Parameters["timeSlotID"].Value ="21";
oleDbCommandCreateAppointment.Parameters["startTime"].Value = "09:00";
oleDbCommandCreateAppointment.Parameters["employeeID"].Value ="1";
oleDbCommandCreateAppointment.Parameters["clientID"].Value ="1";
oleDbCommandCreateAppointment.Parameters["assistantID"].Value ="1";
oleDbCommandCreateAppointment.Parameters["appointmentType"].Value = "Quote";
oleDbCommandCreateAppointment.Parameters["appointmentFlag"].Value = "Booked";
try
{
oleDbConnection.Open();
int rows = oleDbCommandCreateAppointment.ExecuteNonQuery();
MessageBox.Show("Rows inserted " + rows.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
oleDbConnection.Close();
}
SQL命令
INSERT INTO tblAppointments
(appointmentDate, timeSlotID, startTime, employeeID, clientID, assistantID, appointmentType, appointmentFlag)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
非常感謝
您可能想嘗試命名參數,而不是'?'。 JET應該明白你在做什麼。也就是'INSERT INTO ... VALUES(@A,@B,@C)',然後爲你的'OleDbCommand'添加命名參數(例如'@ clientID')。 – 2012-04-06 04:44:45
@ ta.spect.is你不能在OleDbCommand中使用命名參數(我更新了我的答案和關於這個的註釋,它也讓我非常警惕) – pstrjds 2012-04-06 04:48:59
@pstrjds我非常確定JET與它略有不同。你可以說'VALUES(@A,@B,@A)',你只需要按順序添加'@ A'和'@ B'。 – 2012-04-06 21:57:56