码迷,mamicode.com
首页 > Windows程序 > 详细

C#事物执行数据

时间:2014-12-12 18:35:28      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   sp   on   数据   div   log   bs   

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

标签:blog   io   ar   sp   on   数据   div   log   bs   

原文地址:http://www.cnblogs.com/myblogslh/p/4160154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!