C#操作SQLs时使用事务介绍

事务是作为单个逻辑工作单元执行的一系列操作。一个逻辑工作单元必须有四个属性,称为 ACID(原子性、一致性、隔离性和持久性)属性,只有这样才能成为一个事务。事务管理特性,强制保持事务的原子性和一致性。事务启动之后,就必须成功完成,否则SQL Server将撤消该事务启动之后对数据所作的所有修改。

    出于事务的特性(ACID),笔者将应用事务的一致性来排除程序在运行过程中可能发生的一系列“危险”因素,确保数据的安全,例如,一个用户在取钱时,卡以放入取款机进行操作,等待取款,这时突然停电,通过事务可以帮助用户挽回损失,将信息恢复到操作之前的状态。
   主要程序程序代码如下。
首先实例化一个连接对象connection,其次通过连接对象connection又分别实例化一个SqlTransaction对象sqlTran、一个SqlCommand对象command,然后再将command对象的Transaction属性与sqlTran对象关联起来,通过事物实现程序的一致性。

public bool Tran(string strID,string strName,string strSex,string strDelID,bool IfDO) 
        { 
               using (SqlConnection connection = ConDB()); //创建连接对象 
               { 
                SqlTransaction sqlTran = connection.BeginTransaction();//开始事务 
               SqlCommandcommand = connection.CreateCommand();//创建SqlCommand对象 
                command.Transaction = sqlTran;//将SqlCommand与SqlTransaction关联起来 
                try 
                { 
                    command.CommandText = "insert into t_people values('" + strID + "','" + strName +  
                    "','" + strSex + "')"; 
                    command.ExecuteNonQuery(); 
                    command.CommandText = "delete from t_people where tb_PID='" + strDelID + "'"; 
                    command.ExecuteNonQuery(); 
                    if (IfDO)//抛出异常,另其发生回滚 
                    { 
                        throw new Exception(); 
                    } 
                    sqlTran.Commit(); 
                    connection.Close(); 
                    return true; 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine(ex.Message); 
                    sqlTran.Rollback(); 
                    return false; 
                } 
             } 
        }
 
    为Tran方法获取参数,如果Tran方法成功则信息成功添加否则添加失败。为了使读者可以更好的了解事务的工作原理,程序中人为的标识事物的操作方式(提交、回滚)。

private void button1_Click(object sender, EventArgs e) 
        { 
            ClsDB.ClsDBControl DBDT = new OptDB.ClsDB.ClsDBControl(); 
            string strid, strname, strsex, strDelid; 
            strid = this.textBox1.Text.Trim().ToString(); 
            strname = this.textBox2.Text.Trim().ToString(); 
            strsex = this.textBox3.Text.Trim().ToString(); 
            strDelid = this.textBox4.Text.Trim().ToString(); 
            bool Bt; 
            if (this.radioButton1.Checked) 
            { 
                Bt = false; 
            } 
            else 
            { 
                Bt = true; 
            } 
            if (DBDT.Tran(strid, strname, strsex, strDelid, Bt)) 
            { 
                MessageBox.Show("事务供交成功"); 
            } 
            else 
            { 
                MessageBox.Show("事务回滚成功"); 
            } 
        }

作者:佚名
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值