在一个c#方法中使用多个sql命令

本文关键字:sql 命令 一个 方法 | 更新日期: 2023-09-27 18:12:32

范围:学校项目使用:VS-2010,基于服务的数据库经验水平:初学者2个月的基本编码

一般信息:{库存,销售,采购记录}windows(表单)应用系统,基于服务的数据库。

问题:在"添加订单"方法下的"客户订单信息表单"中,我想:

  1. 将表单输入赋值到变量
  2. 使用第一个读取器从"库存详细信息表"中读取产品数量等
  3. 将数据存储在变量
  4. 计算新库存数量和其他需求信息
  5. Sql将表单输入插入到'order details table'
  6. Sql将新数量更新到'Inventory details表'

      private void btnAdd_Click(object sender, EventArgs e)
    { 
        try
        {//
            //1. declare variables
            int OrderID;
            string CusName;
            string GoodsIssued = "";
            // for data entry into table
            string Entry1 = "";
            string Entry2 = "";
            string Entry3 = "";
            // for calulation of new stock level
            int ProductIDA = 0;
            int QuantityOut = 0;
            int OldQty = 0;
            int NewQty = 0;
            int ReorderLvl = 0;
            string ReorderState = "";
            //Boolean error = false;
            //2. get values from textboxes to variable
            OrderID = int.Parse(txtOrderID.Text);
            CusName = txtCustomerName.Text;
            if (rbYes.Checked == true)
            { GoodsIssued = "Yes"; }
            else if (rbNo.Checked == true)
            { GoodsIssued = "No"; }
            else { GoodsIssued = ""; } // further Error reporting possible
            /*due to complication we are compartmentalizing each iteam invoice*/
            Entry1 = txtOQty.Text + "-" + txtItemID.Text;
            Entry2 = txtOQty2.Text + "-" + txtItemID2.Text;
            Entry3 = txtOQty3.Text + "-" + txtItemID3.Text;
            //Sql instert data needs finish here
            //SQL sql Search data needs follow
            ProductIDA = int.Parse(txtItemID.Text);
            //
            //
            //
            string search = "SELECT * FROM tblInventoryDetails WHERE [Product ID] = " + ProductIDA + "";
    
            SqlCommand recall = new SqlCommand(search, con);
            con.Open();
            SqlDataReader reader = recall.ExecuteReader();
    
            while (reader.Read())// Readers need to be read using a loop!!
            {
                //string searchedId = reader[0].ToString();
                //ID  at loction 0 is ommited =can be included
                OldQty = Convert.ToInt32(reader[5]);
                ReorderLvl = Convert.ToInt32(reader[6]);
                ReorderState = reader[7].ToString();
            }
                ////////////////////////////////
            NewQty = OldQty - QuantityOut;
            if (NewQty >= ReorderLvl)   //verify syntax
            { ReorderState = "No"; }
            else { ReorderState = "Yes"; }
            con.Close();
            //3. create the query for entering new values into Order details table
            string insert = "INSERT INTO tblCustomerOrderDetails VALUES (" + OrderID + ", '" + CusName + "', '" + GoodsIssued + "', '" + Entry1 + "', '" + Entry2 + "', '" + Entry3 + "' )";
            string update = "UPDATE tblInventoryDetails SET Quantity= " + NewQty + ", [Re-Order Recommendation]='" + ReorderState + "' WHERE [Product ID] =" + ProductIDA;
            //4. Creating the sql command with query name and connection
            SqlCommand cmdInsert = new SqlCommand(insert, con);
            SqlCommand cmdUpdate = new SqlCommand(update, con);
            //5. Opening connection path
            con.Open();
            //6. Executing the command
            cmdInsert.ExecuteNonQuery();
            cmdUpdate.ExecuteNonQuery();
            //7.Display message if these try is sucessful
            MessageBox.Show("Updated and Saved Sucessfully");
        }// close of try 
        catch (Exception ex)
        {   //8. display error message when its not sucessful
            MessageBox.Show("Error while saving" + ex);
        }// close of catch
        finally
        {  //9.Close the connection
            con.Close();
        }// close of finally
        //
    }
    

这是订单详细信息表单:OrderDetails

更新问题:查询的Update部分,我希望在InventoryDetails表中更新Quantity的新值和重新订购建议,但在运行时似乎没有通过。其他一切似乎都很好。谁能指出我的错误或告诉我为什么更新功能似乎不运行?

观察:这是因为这是第二个[ExecuteNonQuery()]为这个连接?

Thanks in advance

在一个c#方法中使用多个sql命令

在Insert-Statement中,没有对Entry3的引号