三层事物代码
//==============================入库代码================
SqlConnection myConnection = new SqlConnection(tazf.DBUtility.SqlHelper.ConnectionStringLocal);
myConnection.Open();
SqlTransaction myTrans = myConnection.BeginTransaction(); //使用New新生成一个事务
try
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)//
{
kj_pjrkEntity rk = new kj_pjrkEntity();
rk.pjbegin = dataGridView1.Rows[i].Cells[1].Value.ToString().ToString();
rk.pjcode = dataGridView1.Rows[i].Cells[0].Value.ToString().ToString();
rk.pjend = dataGridView1.Rows[i].Cells[2].Value.ToString().ToString();
rk.pjmz = int.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString().ToString());
rk.rkfs = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString().ToString());
rk.pjsum = rk.pjmz * rk.rkfs;
rk.rkcode = label3.Text;
rk.rktime = DateTime.Now;
rk.userid = login.loginid_id;
kj_pjrk_BLLSub.Create_kj_pjrkInsert(myTrans, rk);
}
//主库代码
kj_pjrk_headEntity head = new kj_pjrk_headEntity();
head.rkcode = label3.Text;
head.rktime = DateTime.Now;
head.userid = login.loginid_id;
head.zhidan = "";
kj_pjrk_head_BLLSub.Create_kj_pjrk_headInsert(myTrans, head);
myTrans.Commit();
MessageBox.Show("保存成功!");
}
catch
{
myTrans.Rollback();
MessageBox.Show("保存失败!");
}
finally
{
myConnection.Close();
myTrans.Dispose();
myConnection.Dispose();
}
//================================== |