0

我正在努力更新 MS-ACCESS 数据库中的记录/列值...帮助将不胜感激..!

我正在使用 Datagridview 显示从 Ms-access 中的表中检索到的零件号列表,我应该在其中更新/更改零件号。(“partno”是我的 datagridview 的第三列。)

但我无法更新数据库中的单个记录..没有例外..一切都很好。!

但没有行受到影响!

这是我的代码:

 private void UpdateDetails_Click(object sender, EventArgs e)
 {
      try
      {
          con = new OleDbConnection();
          con.ConnectionString = Helper.MyConnectionString;
          con.Open();

          for (int i = 0; i <= datagridview1.Rows.Count-1; i++)
          {
             int j = i + 1; // j is the serial number corresponding to partnumber

             string partno = dgv1.Rows[i].Cells[2].Value.ToString(); //getting part number from Datagridview

             String partquery = "";

             if (partno == null || partno == "") //checking whether part number updated or not
             {
                 partquery = "update Vendor SET PartNo=NULL where Vendor.Sno=" + j + " ";
             }
             else           
                 partquery = "update Vendor SET PartNo='" + partno + "' where Vendor.Sno=" + j + " ";

             //Vendor is the table name containg 'partno' list

             cmd = new OleDbCommand();
             cmd.Connection = con;
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = partquery;
             cmd.ExecuteNonQuery();
          }
      }
      catch(Exception ex)
          {
        //exception handler
      }

}
4

1 回答 1

1

正如@Soner 建议的那样,您应该使用参数。像这样的东西。

修改了代码你做了这样的事情吗?

    private void UpdateDetails_Click(object sender, EventArgs e)
    {
        try
        {
            con = new OleDbConnection();
            con.ConnectionString = Helper.MyConnectionString;
            con.Open();

            for (int i = 0; i <= datagridview1.Rows.Count - 1; i++)
            {
                int j = i + 1; // j is the serial number corresponding to partnumber

                string partno = dgv1.Rows[i].Cells[2].Value.ToString(); //getting part number from Datagridview

                //String partquery = "";

                //if (partno == null || partno == "") //checking whether part number updated or not
                //{
                //    partquery = "update Vendor SET PartNo=NULL where Vendor.Sno=" + j + " ";
                //}
                //else
                //    partquery = "update Vendor SET PartNo='" + partno + "' where Vendor.Sno=" + j + " ";

                OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("update Vendor SET PartNo='@partno' where Vendor.Sno=@vndid");
                OleDbParameter parameter = new System.Data.OleDb.OleDbParameter("@partno", partno);
                cmd.Parameters.Add(parameter);
                parameter = new System.Data.OleDb.OleDbParameter("@vndid", j);
                cmd.Parameters.Add(parameter);


                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            //exception handler
        }

    }
于 2013-09-12T07:07:05.127 回答