T-SQL中的事务处理

begin transaction
    --使用try…catch结构捕获异常
    begin try
        --插入两条数据(相同的用户名)
        INSERT INTO [test].[dbo].[Users]
                   ([UserName]
                   ,[Password])
             VALUES
                   ('张三','123')
        INSERT INTO [test].[dbo].[Users]
                   ([UserName]
                   ,[Password])
             VALUES
                   ('张三','456')
        commit transaction --提交事务
    end try
    --如果出现了异常,进入catch代码段
    begin catch
        rollback transaction --回滚事务
        select ERROR_MESSAGE() as [Message] --输出错误信息
    end catch

 

ADO.NET中的事务处理

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlCommand command = connection.CreateCommand();
    SqlTransaction transaction;
    // 启动一个本地事务
     transaction = connection.BeginTransaction();
    // 在启动一个本地事务之前,需要为Command对象指派Connection对象与Transaction对象
     command.Connection = connection;
    command.Transaction = transaction;
    try
    {
        command.CommandText =
            "INSERT INTO [test].[dbo].[Users] ([UserName] ,[Password]) VALUES ('张三','123')";
        command.ExecuteNonQuery();
        command.CommandText =
            "INSERT INTO [test].[dbo].[Users] ([UserName] ,[Password]) VALUES ('张三','123')";
        command.ExecuteNonQuery();
        // 尝试提交事务
         transaction.Commit();
        Console.WriteLine("所有的记录已经提交至数据库");
    }
    catch (Exception ex)
    {
        Console.WriteLine("引发的异常类型为: {0}", ex.GetType());
        Console.WriteLine("异常信息: {0}", ex.Message);
        // 尝试回滚事务
        try
        {
            transaction.Rollback();
        }
        catch (Exception ex2)
        {
            // 此catch块将处理任何可能发生在服务器上的,导致回滚失败的错误。如连接已关闭。
             Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
            Console.WriteLine("  Message: {0}", ex2.Message);
        }
    }
}

 

LINQ to SQL中的隐式事务处理

using (TestDataContext db = new TestDataContext())
{
    // 在控制台中输出T-SQL语句
    db.Log = Console.Out;
    //创建两个Users对象(注意,用户名是相同的)
    Users u1 = new Users()
    {
        UserName = "张三",
        Password = "123"
    };
    Users u2 = new Users()
    {
        UserName = "张三",
        Password = "456"
    };

    try
    {
        db.Users.InsertAllOnSubmit(new Users[] { u1, u2 });
        // 尝试提交事务
         db.SubmitChanges();
        Console.WriteLine("所有的记录已经提交至数据库");
    }
    catch (Exception ex)
    {
        Console.WriteLine("引发的异常类型为: {0}", ex.GetType());
        Console.WriteLine("异常信息: {0}", ex.Message);
    }
}


 

分布式事务处理

//创建两个TestDataContext对象
TestDataContext db1 = new TestDataContext();
TestDataContext db2 = new TestDataContext();
//创建两个Users对象(注意,用户名是相同的)
Users u1 = new Users()
{
    UserName = "张三",
    Password = "123"
};
Users u2 = new Users()
{
    UserName = "张三",
    Password = "456"
};
//将两个Users对象分别加入到不同的TestDataContext对象中
db1.Users.InsertOnSubmit(u1);
db2.Users.InsertOnSubmit(u2);
// 使用TransactionScope对象,使代码块成为事务性代码。
using (TransactionScope rs = new TransactionScope())
{
    try
    {
        // 尝试提交事务
         db1.SubmitChanges();
        db2.SubmitChanges();
        rs.Complete();
        Console.WriteLine("所有的记录已经提交至数据库");
    }
    catch (Exception ex)
    {
        Console.WriteLine("引发的异常类型为: {0}", ex.GetType());
        Console.WriteLine("异常信息: {0}", ex.Message);
    }
}

 

Posted on 2010-01-25 12:30  Yongming Ye  阅读(176)  评论(0)    收藏  举报