微软企业库 5 启用事务回滚

            SqlDatabase db = new SqlDatabase(@"server=.\sql2008;uid=sa;pwd=123456;database=DBTEST");

            
using (DbConnection connection = db.CreateConnection())
            {
                
//打开链接
                connection.Open();
                
//创建事务
                DbTransaction Tran = connection.BeginTransaction();
                
try
                {

                    db.ExecuteNonQuery(Tran, CommandType.Text, 
"Insert Into TLog (DEC) Values('123')");
                    db.ExecuteNonQuery(Tran, CommandType.Text, 
"Insert Into TLog (DEC) Values('124')");
                    db.ExecuteNonQuery(Tran, CommandType.Text, 
"Insert Into TLog (DEC) Values(125)");
                    
//提交事务
                    Tran.Commit();
                }
                
catch (Exception Ex)
                {
                    
//出错回滚
                    Tran.Rollback();
                    Console.WriteLine(Ex.ToString());
                }
                
finally
                {
                    
//关闭连接
                    connection.Close();
                }

                Console.WriteLine(
"OK");
                Console.ReadKey();
            }

 

不要忘记在执行时加入“Tran”,否则不起做用的

上一篇:HBaseCon亚洲2018峰会盛大开幕 阿里带你洞悉HBase大数据生态最新发展和行业实践


下一篇:配置Exchange 2010 服务器(二)Exchange2010证书配置