正在将记录插入Access数据库

本文关键字:Access 数据库 插入 记录 | 更新日期: 2023-09-27 18:30:07

我在向访问数据库中插入记录时遇到了很大困难。我在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        (?, ?, ?, ?, ?, ?, ?, ?)

非常感谢

正在将记录插入Access数据库

您需要将连接与db命令对象相关联:

oleDbConnection.Open();
oleDbCommandCreateAppointment.Connection = oleDbConnection;
int rows = oleDbCommandCreateAppointment.ExecuteNonQuery();

编辑-这个可能有与参数排序有关,试试这个:

oleDbCommandCreateAppointment.Parameters[0].Value = "06/04/2012";
oleDbCommandCreateAppointment.Parameters[1].Value = "21";
oleDbCommandCreateAppointment.Parameters[2].Value = "09:00";
oleDbCommandCreateAppointment.Parameters[3].Value = "1";
oleDbCommandCreateAppointment.Parameters[4].Value = "1";
oleDbCommandCreateAppointment.Parameters[5].Value = "1";
oleDbCommandCreateAppointment.Parameters[6].Value = "Quote";
oleDbCommandCreateAppointment.Parameters[7].Value = "Booked";

注意-我在处理此问题时了解到,不能将命名参数与ODBC和OleDB命令一起使用,只能使用位置参数(请参见表6),并且此链接指出,当命令类型为Text时,OleDbCommand对象仅支持位置参数。位置参数取决于顺序。