c # Affairs essay

Once upon a time, how exciting half-day operational matters.

Once upon a time, he wrote such transactions elated!

 

string connStr=@"Data Source=.\sqlexpress;Initial Catalog=DbTest;Integrated Security=True";
            using(SqlConnection conn=new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.Transaction = trans;
                    try
                    {
                        //操作1
                        cmd.CommandText = "insert into test1(id) values('aa')";
                        cmd.ExecuteNonQuery();

                        //操作2
                        cmd.CommandText = "insert into test1(id) values('01234567890')";//该句出错,id类型为varchar(10)
                        cmd.ExecuteNonQuery();

                        trans.Commit();
                        Console.WriteLine("提交!!");
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine(e.Message);
                        trans.Rollback();
                        Console.WriteLine("回滚!!");
                    }
                }
                Console.ReadLine();
            }
附上只有一个字段的Test1表:
ID varchar(10) 主键

而后,又发现了Linq to Sql 这个好东西,它是一个轻量级级的ORM,功能非常强大,而且自带事务集成功能。简单的Linq参考资料

我们同样操作上面一个过程,使用Linq如下

1.新建一个 Linq to Sql 类,命名为DataClasses1.dbml,再通过‘服务器资源管理器’ 把数据库中的表添加到设计画面。

2.事务操作如下。

            DataClasses1DataContext db = new DataClasses1DataContext();

            try
            {
                //插入一条ID=1的Test1 的实体。
                db.Test1.InsertOnSubmit(new Test1() { ID = "1" });

                //插入一条超出限定长度的数据,让它报错
                db.Test1.InsertOnSubmit(new Test1() { ID = "01234567890" });

                //提交
                db.SubmitChanges();
            }
            catch { }

Obviously, because of the time of filing of the second error statement, the commit fails, the first statement is also rolled back. He's such a simple operation, but we also ORM model is no longer a headache, do not have to write so original and easy to forget the transaction. That it is not perfect it?

 

Actually not, we can imagine, the transaction is a cross-database operations if it is clearly just use Linq can not be solved. Also, Linq not yet mature, for different databases, produce inconsistent operation, similar to browser compatibility issues. But Linq is really powerful, it does not affect the existence of God.

 

Recently, the transaction has to find a TransactionScope class, TransactionScope is like a mirror, you just shout at him: God, grant me the transaction! You really get the business.

As follows:

 

            using (TransactionScope scope = new TransactionScope())
            { 
                //操作1,数据库1操作

                //操作2,数据库2操作

                //操作3
                scope.Complete();
            }

For Scope.Complete any abnormal operation before (), all operations are rolled back (whether it belongs to which database server).

So powerful, that he is the mirror that is no exaggeration!

 

 

 

Reproduced in: https: //www.cnblogs.com/xinjian/archive/2010/07/09/1774033.html

Guess you like

Origin blog.csdn.net/weixin_34209406/article/details/93822279