微软企业库回滚操作

            SqlDatabase db = new SqlDatabase(@"server=.\sql2008;uid=sa;pwd=abc@123;database=EFDemo");

            DbConnection dbconn 
= db.CreateConnection();
            dbconn.Open();
            DbTransaction Idbtran 
= dbconn.BeginTransaction();
            
try
            {
                DbCommand sql 
= db.GetSqlStringCommand("Insert Into [Author](FirstName,LastName) Values('han1','wei1')");
                db.ExecuteNonQuery(sql,Idbtran);
                DbCommand sql1 
= db.GetSqlStringCommand("Update [Author] Set LastName = 'wei1xiu' where FirstName='han1'");
                db.ExecuteNonQuery(sql1,Idbtran);

                Idbtran.Commit();
            }
            
catch
            {
                Idbtran.Rollback();
            }
            
finally
            {
                dbconn.Close();
            }
            Console.WriteLine(
"OK");
            Console.ReadKey();
posted @ 2011-04-25 13:05  韩天伟  阅读(377)  评论(0编辑  收藏  举报