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 ExpandedBlockStart.gifContractedBlock.gif             {
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 ExpandedBlockStart.gifContractedBlock.gif             {
23                myTrans.Rollback();
24                MessageBox.Show("写入数据库失败!");
25            }

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

ado.net2.0的简单事务处理:(using System.Transactions;)
 1 string  strCon  =   " Server=(local);Initial Catalog=Demo24;uid=sa;pwd=111; " ;
 2              try
 3 ExpandedBlockStart.gifContractedBlock.gif             {
 4
 5                using (TransactionScope ts = new TransactionScope())
 6ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 7                    using (SqlConnection myConnection = new SqlConnection(strCon))
 8ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 9                        myConnection.Open();
10                        using (SqlCommand myCommand = myConnection.CreateCommand())
11ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
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 ExpandedBlockStart.gifContractedBlock.gif             {
25                MessageBox.Show("程序出错!事务没有成功!");
26            }

ado.net2.0的分布式事务处理:(using System.Transactions;)
 1       using  (TransactionScope ts  =   new  TransactionScope())
 2 ExpandedBlockStart.gifContractedBlock.gif                 {
 3                    using (SqlConnection myConnection = new SqlConnection(strCon))
 4ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 5                        myConnection.Open();
 6                        using (SqlCommand myCommand = myConnection.CreateCommand())
 7ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
 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))
15ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
16                        myConnection.Open();
17                        using (SqlCommand myCommand = myConnection.CreateCommand())
18ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
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 ExpandedBlockStart.gifContractedBlock.gif             {
31                MessageBox.Show("程序出错!事务没有成功!");
32            }

转载于:https://www.cnblogs.com/ewyb/archive/2009/09/09/1563491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值