【问题标题】:Insert into table 1 update table 2 on button click in c# using access在 c# 中使用访问按钮单击按钮插入表 1 更新表 2
【发布时间】:2013-11-08 06:39:08
【问题描述】:

好的,这是我的问题。我正在为工作制作一个新的 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();

【问题讨论】:

  • 它归结为 cmd1.executenonquery();我得到 ExecuteNonQuery: Connection property has not been initialized.
  • 这很奇怪,因为您没有使用任何连接对象初始化您的 oleDBCommand 对象 cmd1,请执行以下操作: cmd1.Connection=con;在执行 cmd1 之前。

标签: c# ms-access multiple-tables


【解决方案1】:

你没有执行cmd1

cmd1.ExecuteNonQuery();

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

【讨论】:

    【解决方案2】:

    失败原因:你只是在执行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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,? ,?)");
    

    【讨论】:

    • Sudhakar,我试过这个,我得到这个错误 {"Characters found after end of SQL statement."}
    • 您是否在插入命令后在字符串中添加了分号,如我的答案所示?
    • 是的,这就是它抛出该错误的时候。我复制了你的答案并粘贴了它。然后我也尝试以另一种方式恢复并执行 cmd1 并得到另一个错误
    • @xgonnahaveitx:请查看我更新后的答案 3:希望对您有所帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 2013-03-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多