在单个方法 C# 中使用多个 sql 命令
Using multiple sql commands in a single method C#
范围:学校项目
使用:VS-2010,基于服务的数据库
经验水平:初学者 - 仅 2 个月的基本编码
一般信息:{库存、销售、采购记录}windows(表格)具有基于服务的数据库的应用系统。
问题:"In the Customer Order Info form' under method to "添加订单”我想:
- 将表单输入分配给变量
- 使用第一个reader从'Inventory details table'读取产品数量等
- 将数据存储在变量中
- 计算新库存数量和其他需要的信息
- Sql 将表单输入插入 'order details table'
Sql 将新数量更新到 'Inventory details table'
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
更新问题:
当 运行 时,我希望在 InventoryDetails table 中更新数量和重新订购建议的新值的查询的更新部分似乎没有通过。其他一切似乎工作正常。
谁能指出我的错误或告诉我为什么 Update 函数似乎没有 运行?
观察:是不是因为这是这个连接的第二个[ExecuteNonQuery()]?
提前致谢
在您的 Insert-Statement 中,Entry3 没有引号。
范围:学校项目 使用:VS-2010,基于服务的数据库 经验水平:初学者 - 仅 2 个月的基本编码
一般信息:{库存、销售、采购记录}windows(表格)具有基于服务的数据库的应用系统。
问题:"In the Customer Order Info form' under method to "添加订单”我想:
- 将表单输入分配给变量
- 使用第一个reader从'Inventory details table'读取产品数量等
- 将数据存储在变量中
- 计算新库存数量和其他需要的信息
- Sql 将表单输入插入 'order details table'
Sql 将新数量更新到 'Inventory details table'
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
更新问题: 当 运行 时,我希望在 InventoryDetails table 中更新数量和重新订购建议的新值的查询的更新部分似乎没有通过。其他一切似乎工作正常。 谁能指出我的错误或告诉我为什么 Update 函数似乎没有 运行?
观察:是不是因为这是这个连接的第二个[ExecuteNonQuery()]?
提前致谢
在您的 Insert-Statement 中,Entry3 没有引号。