• c#事务用法


    ado.net2.0的SqlTransaction使用方法
    /////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+的开销。
    */
    

      

  • 相关阅读:
    background image position问题
    yii 验证器和验证码
    laravel 模板 blade
    tbody添加垂直滚动条
    转:jquery选择器总结
    jquery ajax传递数组给php
    jquery serialize()、serializearray()已经$.param方法
    php stdClass Object 问题
    Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)
    HTTP协议具体解释
  • 原文地址:https://www.cnblogs.com/lhws/p/3533926.html
Copyright © 2020-2023  润新知