sql事务处理并发问题,求大神讲解啊,最好给个例子
搜索懂不懂?不懂问老师去!
需求;假设AB2个表,分别有设有主键自增长,
A:AID,AName
B:BID,BName,AID
看表结构应该能看懂吧。
事务插入代码(c#)如下:
SqlTransaction tran = null; try { string connectionString = ConfigurationManager.ConnectionStrings["connctionString"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); tran = conn.BeginTransaction(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.Transaction = tran; cmd.CommandText = "insert into A values(@AName);select @@Identity"; cmd.Parameters.Add("@AName", "testName"); int id = Convert.ToInt32(cmd.ExecuteScalar()); cmd.CommandText = "insert into B values(@BName,@AID)"; cmd.Parameters.Add("@BName", "testName"); cmd.Parameters.Add("@AID", id); cmd.ExecuteNonQuery(); MessageBox.Show("插入成功"); } tran.Commit(); } } catch (Exception ex) { tran.Rollback(); throw; }
你百度下并发处理.或者资源竞争解决方法,跟sql不sql关系不大