【问题标题】:Using multiple sql commands in a single method C#在单个方法 C# 中使用多个 sql 命令
【发布时间】:2016-10-10 17:22:03
【问题描述】:

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

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

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

  1. 将表单输入分配给变量
  2. 使用第一个阅读器从“库存明细表”中读取产品数量等
  3. 将数据存储在变量中
  4. 计算新库存数量和其他需求信息
  5. Sql to INSERT form input into ‘order details table’
  6. 将新数量更新到“库存明细表”的 Sql

      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

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

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

提前致谢

【问题讨论】:

  • 返回什么错误?
  • 我认为是因为 Entry3 周围缺少单引号
  • 不要连接字符串:使用参数。另外:您应该围绕 2 个语句使用事务,因为它们会更改 2 个不同表中的状态

标签: c# sql-server visual-studio-2010


【解决方案1】:

在您的插入语句中,Entry3 没有引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多