如何使用自动增量saleID更新第三个表?
我的代码无效。
string query1 = "Update item set qty = '" + final + "'where itemId ='" + item + "'";
int rows = db.SaveUpdateDelete(query1);
DateTime d = DateTime.Now;
int day = d.Day;
int month = d.Month;
int year = d.Year;
string dt = year + "/" + month + "/" + day;
string query2 = "Insert into sale (empID,date) values ('" + usename + ",'" + dt + "')";
int rows1 = db.SaveUpdateDelete(query2);
string query3 = "Insert into sale_item (itemId, qty, unit_price) values ('" + item + "','" + qty + "','" + price + "'";
int rows2 = db.SaveUpdateDelete(query3);
答案 0 :(得分:0)
在第2张表销售中完成插入后,从销售表执行前1 基于id。 从该输出中获取销售ID,插入第三个表。用虚拟值执行下面的代码并且它有效。
var conn = new SqlConnection("connection string");
conn.Open();
SqlCommand comm = new SqlCommand("Update item set qty = '" + 30 + "'where itemId ='" + 1 + "'",conn);
comm.ExecuteNonQuery();
DateTime d = DateTime.Now;
int day = d.Day;
int month = d.Month;
int year = d.Year;
string dt = year + "/" + month + "/" + day;
string username = "jyothi";
SqlCommand query2 = new SqlCommand("Insert into sale (empID,date1) values ('" + 22 + "','" + dt + "')",conn);
query2.ExecuteNonQuery();
int saleid=0;
SqlCommand query3 = new SqlCommand("select top 1 id from sale order by id desc",conn);
var dreader = query3.ExecuteReader();
if (dreader.Read())
{
saleid = Convert.ToInt32(dreader[0].ToString());
}
dreader.Close();
SqlCommand query4 = new SqlCommand("Insert into saleitem (saleid,itemId, qty, price) values ('" + saleid + "','" + 1 + "','" + 20 + "','" + 25 + "')", conn);
query4.ExecuteNonQuery();