1

我在将记录插入我的访问数据库时遇到了很大的困难。我已经在访问中尝试了查询,它插入得很好。我还尝试了查询生成器中的查询,它也可以工作,但是如果我运行此代码,它声称已将记录插入数据库,但是当我检查数据库时没有新记录的迹象。

      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        (?, ?, ?, ?, ?, ?, ?, ?)

非常感谢

4

1 回答 1

3

您需要将连接与 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),并且此链接指出OleDbCommand对象仅在命令类型为 时支持位置参数Text。位置参数取决于顺序。

于 2012-04-06T03:37:01.980 回答