0

好的,这是我的问题。我正在为工作制作一个新的 GUI。没什么特别的,只是输入要打印在标签上的信息。我已经设法让代码在单击按钮时将值插入到 MainLabel 表中,但我还需要这些来更新 Label 表中的第一行。

由于某种原因,它会插入但不进行更新。我怎样才能做到这一点?这是我有任何帮助将不胜感激。

更新了还是不行

     OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jim\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\labels1.mdb;Persist Security Info=False";

        OleDbCommand cmd = new OleDbCommand();
        OleDbCommand cmd1 = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd1.CommandType = CommandType.Text;
        cmd1.CommandText = "UPDATE Label SET SaleOrderNumber = @SaleOrderNumber, NsN = @NsN, NsNBarcode = @NsNBarcode, PartNumber = @PartNumber, Qty = @Qty, Description = @Description, CustomerPo = @CustomerPo, CustomerPoBarcode = @CustomerPoBarcode, PackingCode = @PackingCode, Weight = @Weight, Clin = @Clin, SaleOrderDate = @SaleOrderDate, MCM = @MCM, Cage = @Cage ";

        cmd.CommandText = "INSERT into MainLabel ([SaleOrderNumber], [NsN], [NsNBarcode], [PartNumber], [Qty], [Description], [CustomerPo], [CustomerPoBarcode], [PackingCode], [Weight], [Clin], [SaleOrderDate], [MCM], [Cage]) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,? ,?)";



        cmd.Parameters.AddWithValue("@SaleOrderNumber", label1.Text);
        cmd.Parameters.AddWithValue("@NsN", label6.Text);
        cmd.Parameters.AddWithValue("@NsNBarcode", label6.Text);
        cmd.Parameters.AddWithValue("@PartNumber", label2.Text);
        cmd.Parameters.AddWithValue("@Qty", label7.Text);
        cmd.Parameters.AddWithValue("@Description", label3.Text);
        cmd.Parameters.AddWithValue("@CustomerPo", label8.Text);
        cmd.Parameters.AddWithValue("@CustomerPoBarcode", label8.Text);
        cmd.Parameters.AddWithValue("@PackingCode", label4.Text);
        cmd.Parameters.AddWithValue("@Weight", label9.Text);
        cmd.Parameters.AddWithValue("@Clin", label5.Text);
        cmd.Parameters.AddWithValue("@SaleOrderDate",label12.Text);
        cmd.Parameters.AddWithValue("@MCM", label10.Text);
        cmd.Parameters.AddWithValue("@Cage", label11.Text);
        cmd.Connection = con;




        con.Open();

        cmd.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
        con.Close();
4

2 回答 2

1

你没有执行cmd1.

cmd1.ExecuteNonQuery();

旁注,使用语句中的所有IDisposable对象。using

于 2013-11-08T06:43:42.280 回答
1

失败原因:你只是在执行OleDBCommand对象cmd,而不是执行 cmd1给出更新命令的地方。

解决方案1: 您必须执行OleDBCommand对象cmd1(更新命令),如下所示:

cmd1.ExecuteNonQuery();

解决方案2:

您可以将插入和更新命令组合成一个字符串并执行一次,如下所示。

String command="";
command= "INSERT into MainLabel ([SaleOrderNumber], [NsN], [NsNBarcode], [PartNumber], [Qty], [Description], [CustomerPo], [CustomerPoBarcode], [PackingCode], [Weight], [Clin], [SaleOrderDate], [MCM], [Cage]) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,? ,?);";
command+="UPDATE Label SET SaleOrderNumber = @SaleOrderNumber, NsN = @NsN, NsNBarcode = @NsNBarcode, PartNumber = @PartNumber, Qty = @Qty, Description = @Description, CustomerPo = CustomerPo, CustomerPoBarcode = @CustomerPoBarcode, PackingCode = @PackingCode, Weight = @Weight, Clin = @Clin, SaleOrderDate = @SaleOrderDate, MCM = @MCM, Cage = @Cage ";

cmd.CommanText=command;

cmd.ExecuteNonQuery();

注意:在两个命令之间添加分号。

解决方案 3: 创建一个函数来执行您的 sql 命令 - 这将负责创建和处理您的 OleDB 连接和命令对象。

private  void RunMyCommand(String SQLCommand)
        {
            using (OleDbConnection con = new OleDbConnection())
            {
                con.ConnectionString =
                    @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jim\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\labels1.mdb;Persist Security Info=False";
                con.Open();
                using (OleDbCommand cmd = new OleDbCommand())
                {

                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = SQLCommand;
                    cmd.Parameters.AddWithValue("@SaleOrderNumber", label1.Text);
                    cmd.Parameters.AddWithValue("@NsN", label6.Text);
                    cmd.Parameters.AddWithValue("@NsNBarcode", label6.Text);
                    cmd.Parameters.AddWithValue("@PartNumber", label2.Text);
                    cmd.Parameters.AddWithValue("@Qty", label7.Text);
                    cmd.Parameters.AddWithValue("@Description", label3.Text);
                    cmd.Parameters.AddWithValue("@CustomerPo", label8.Text);
                    cmd.Parameters.AddWithValue("@CustomerPoBarcode", label8.Text);
                    cmd.Parameters.AddWithValue("@PackingCode", label4.Text);
                    cmd.Parameters.AddWithValue("@Weight", label9.Text);
                    cmd.Parameters.AddWithValue("@Clin", label5.Text);
                    cmd.Parameters.AddWithValue("@SaleOrderDate", label12.Text);
                    cmd.Parameters.AddWithValue("@MCM", label10.Text);
                    cmd.Parameters.AddWithValue("@Cage", label11.Text);
                    cmd.Connection = con;
                    cmd.ExecuteNonQuery();
                }

            }

        }

调用上述函数如下:

 RunMyCommand("UPDATE Label SET SaleOrderNumber = @SaleOrderNumber, NsN = @NsN, NsNBarcode = @NsNBarcode, PartNumber = @PartNumber, Qty = @Qty, Description = @Description, CustomerPo = @CustomerPo, CustomerPoBarcode = @CustomerPoBarcode, PackingCode = @PackingCode, Weight = @Weight, Clin = @Clin, SaleOrderDate = @SaleOrderDate, MCM = @MCM, Cage = @Cage ");
 RunMyCommand("INSERT into MainLabel ([SaleOrderNumber], [NsN], [NsNBarcode], [PartNumber], [Qty], [Description], [CustomerPo], [CustomerPoBarcode], [PackingCode], [Weight], [Clin], [SaleOrderDate], [MCM], [Cage]) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,? ,?)");
于 2013-11-08T06:47:12.710 回答