首页 > 代码库 > C#事物执行数据
C#事物执行数据
public class sqlservershiwu { public string sqlconString = "Data Source=.;Initial Catalog=TestDB;User ID=sa;Password=123456789"; public void findAll() { bool flag = false; int result = 0; using (SqlConnection con = new SqlConnection(sqlconString)) { con.Open(); SqlCommand com = new SqlCommand(); com.CommandType = System.Data.CommandType.Text; com.Connection = con; //共享锁 SqlTransaction st = con.BeginTransaction(IsolationLevel.ReadCommitted); //开始执行事务 com.Transaction = st; try { string sql1 = "delete from testTable where id = 1"; com.CommandText = sql1; result += com.ExecuteNonQuery(); string sql2 = "delete from testTable where id = 2"; com.CommandText = sql2; result += com.ExecuteNonQuery(); string sql3 = "delete from testTable where id = q"; com.CommandText = sql3; result += com.ExecuteNonQuery(); string sql4 = "insert into testTable(id,name)values(‘3‘,‘3‘)"; com.CommandText = sql4; result += com.ExecuteNonQuery(); //提交事务 st.Commit(); flag = true; } catch (Exception ex) { flag = false; //事务回滚 st.Rollback(); throw ex; } } } }
C#事物执行数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。