zoukankan      html  css  js  c++  java
  • ADO.NET2.0中的事务处理

    ado.net1.X的事务处理
     1  SqlConnection myConnection = new SqlConnection("Server=(local);Initial Catalog=Demo24;uid=sa;pwd=111;");
     2            myConnection.Open();
     3            // 启动一个事务
     4            SqlTransaction myTrans = myConnection.BeginTransaction();
     5
     6
     7            // 为事务创建一个命令
     8            SqlCommand myCommand = new SqlCommand();
     9            myCommand.Connection = myConnection;
    10            myCommand.Transaction = myTrans;
    11            try
    12            {
    13                myCommand.CommandText = "Insert into tbUserInfo (UserName, UserPass,Birthday,Score) VALUES ('成龙', '111','1966-1-1',540)";
    14                myCommand.ExecuteNonQuery();
    15                //myTrans.Commit();
    16                myCommand.CommandText = "Insert into tbUserInfo (UserName, UserPass,Birthday,Score) VALUES ('王五', '222','198834',550)";
    17                myCommand.ExecuteNonQuery();
    18                myTrans.Commit();
    19                MessageBox.Show("成功写入记录!");
    20            }

    21            catch 
    22            {
    23                myTrans.Rollback();
    24                MessageBox.Show("写入数据库失败!");
    25            }

    26            finally
    27            {
    28                myConnection.Close();
    29            }

    ado.net2.0的简单事务处理:(using System.Transactions;)
     1string strCon = "Server=(local);Initial Catalog=Demo24;uid=sa;pwd=111;";
     2            try
     3            {
     4
     5                using (TransactionScope ts = new TransactionScope())
     6                {
     7                    using (SqlConnection myConnection = new SqlConnection(strCon))
     8                    {
     9                        myConnection.Open();
    10                        using (SqlCommand myCommand = myConnection.CreateCommand())
    11                        {
    12
    13                            myCommand.CommandText = "Insert into tbUserInfo (UserName, UserPass,Birthday,Score) VALUES ('成龙', '111','1966-1-1',540)";
    14                            myCommand.ExecuteNonQuery();
    15                            //myTrans.Commit();
    16                            myCommand.CommandText = "Insert into tbUserInfo (UserName, UserPass,Birthday,Score) VALUES ('王五', '222','1988-3-4',550)";
    17                            myCommand.ExecuteNonQuery();
    18                            ts.Complete();                            
    19                        }

    20                    }

    21                }

    22            }

    23            catch
    24            {
    25                MessageBox.Show("程序出错!事务没有成功!");
    26            }

    ado.net2.0的分布式事务处理:(using System.Transactions;)
     1     using (TransactionScope ts = new TransactionScope())
     2                {
     3                    using (SqlConnection myConnection = new SqlConnection(strCon))
     4                    {
     5                        myConnection.Open();
     6                        using (SqlCommand myCommand = myConnection.CreateCommand())
     7                        {
     8
     9                            myCommand.CommandText = "Select count(*) from tbUserInfo";
    10                            int nCount = (int)myCommand.ExecuteScalar();
    11                            MessageBox.Show(nCount.ToString());
    12                        }

    13                    }

    14                    using (SqlConnection myConnection = new SqlConnection(strCon))
    15                    {
    16                        myConnection.Open();
    17                        using (SqlCommand myCommand = myConnection.CreateCommand())
    18                        {
    19
    20                            myCommand.CommandText = "Select count(*) from tbUserInfo";
    21                            int nCount = (int)myCommand.ExecuteScalar();
    22                            MessageBox.Show(nCount.ToString());
    23                            
    24                        }

    25                    }

    26                    ts.Complete();
    27                }

    28            }
    29            catch
    30            {
    31                MessageBox.Show("程序出错!事务没有成功!");
    32            }
  • 相关阅读:
    jquery 拼图小游戏
    重要参考SQL
    SQL Server save transaction
    SelectList类的构造函数
    一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程重要
    springMVC,spring和Hibernate整合(重要)
    delphi环境变量
    C# Chart 点击获取当前点击坐标和Series
    如何修改 app.config 的配置信息
    C#中使用设置(Settings.settings) Properties.Settings.Default .(配置文件相当重要)
  • 原文地址:https://www.cnblogs.com/ewyb/p/1563491.html
Copyright © 2011-2022 走看看