• C# 使用事务


     

    /////ado.net1.0中使用Transacation(事务)
                string connectionString = "database=Northwind;pwd=admin;uid=sa;server=DATABASE-VPC\SQL2005";
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlTransaction transaction = conn.BeginTransaction();
                    try
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.Connection = conn;
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = "";
                            cmd.Transaction = transaction;
                            cmd.ExecuteNonQuery();
                            transaction.Commit();//提交事务
                        }
                    }
                    catch
                    {
                        transaction.Rollback();//事务回滚
                    }
     
     
     
    /////2.0中使用事务
                string connectionString = "database=Northwind;pwd=admin;uid=sa;server=DATABASE-VPC\SQL2005";
                string connectionStringTwo = "database=Test;pwd=admin;uid=sa;server=DATABASE-VPC\SQL2005";
                using (TransactionScope transaction = new TransactionScope())//使用事务
                {
                    try
                    {
                        using (SqlConnection conn = new SqlConnection(connectionString))
                        {
                            using (SqlCommand cmd = new SqlCommand())
                            {
                                cmd.CommandText = "insert into [member] (name) values ('test')";
                                cmd.CommandType = CommandType.Text;
                                cmd.Connection = conn;
                                conn.Open();
                                cmd.ExecuteNonQuery();
                            }
                        }
                        using (SqlConnection conn = new SqlConnection(connectionStringTwo))
                        {
                            using (SqlCommand cmd = new SqlCommand())
                            {
                                cmd.CommandText = "insert into [member] (name) values ('test')";
                                cmd.CommandType = CommandType.Text;
                                cmd.Connection = conn;
                                conn.Open();
                                cmd.ExecuteNonQuery();
                            }
                        }
                        transaction.Complete();//就这句就可以了。
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
     
     
    /*ado.net1.0与ado.net2.0中对比
    1.0中不能跨数据库,速度慢
    2.0中可以跨数据库,2.0中不再使用com+,速度比1.0快50%(因为它不用把大量的时间消耗在查找在com+中)
    2.0中的事务会自动判断使用何种事务(不用您考虑是简单事务还是分部式事务)。
    2.0中的Transactions新添加了命名空间。没有com+注册和com+的开销。
    */
     
     
    转载地址:https://www.cnblogs.com/lhws/p/3533926.html
  • 相关阅读:
    用CSS开启硬件加速来提高网站性能
    vim中替换内容
    alias vi=vim
    PHP 多进程初识
    端口的查看
    PHP三种终止脚本执行:return,die,exit
    2021.3.14(每周总结)
    2021.3.13
    2021.3.12
    2021.3.11
  • 原文地址:https://www.cnblogs.com/clj0102/p/9303226.html
Copyright © 2020-2023  润新知