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#事物执行数据

上一篇:windows下用c实现Socket通信


下一篇:C# 学习笔记