【问题标题】:Inserting a record into Access Database将记录插入 Access 数据库
【发布时间】:2012-04-06 03:30:22
【问题描述】:

我在向我的 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),然后将命名参数(例如@clientID)添加到您的OleDbCommand
  • @ta.spect.is 您不能将命名参数与 OleDbCommand 一起使用(我更新了我的答案并附上了一个注释,这也让我措手不及)
  • @pstrjds 我很确定 JET 的做法略有不同。你可以说VALUES (@A, @B, @A),只需要依次加上@A@B
  • @ta.speot.is 这不是底层引擎的问题,你可以在 Jet 中使用命名值,他使用的是 OleDB 连接器,它只支持位置参数。我在回答中提供了一个链接。
  • @pstrjds 是的,我记得Microsoft.Jet.OLEDB.4.0 在您通过System.Data.OleDb 使用它时支持命名参数。

标签: c# sql ms-access insert


【解决方案1】:

需要将连接关联到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 命令一起使用,只能使用位置参数(请参阅 Table 6),并且此链接指出 OleDbCommand 对象仅支持位置参数当命令类型为Text 时。位置参数取决于顺序。

【讨论】:

  • 完成了,但仍然有同样的问题
  • 从您发布的代码来看,它看起来不像您拥有的,您能否展示更多您的代码、命令对象的构造等。您是在打开事务而不提交它吗?
  • 对不起,我的意思是我已经添加了你的建议。我的连接对象位于表单的托盘区域,我的插入命令也是如此。
  • 您至少可以发布您的查询吗?你的连接字符串怎么样。
  • 参数顺便存到集合中*
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多