.NET中使用C#进行的交易 [英] Transactions in .net using c#

查看:85
本文介绍了.NET中使用C#进行的交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
.net中的交易

我只是想了解使用c#语言在.net中进行交易.我在网上浏览了不同的文章.但是,我从理论上开始了解交易,但是我想实时了解它的确切用法.例如,确切的时间我应该实时使用事务.让我们假设,我正在编写代码,对链接的click事件执行一些操作.可以说,我正在通过SQL连接来获取一些值.我应该在那里使用交易吗?如果我正在编写简单的代码,而无需使用sql连接来获取值,那么我应该在其中使用事务吗?使用事务的优缺点是什么?获得理论知识是不同的,但是我想知道它的确切用法.何时使用,何时不使用.交易可以用简单的代码使用吗?我们欢迎您提供任何交易或什至有关交易基本内容的链接.

I just wanted to know about Transaction in .net using c# language. I have gone through different articles on the net. However, I came to know about transaction theoretically but I wanted to know the exact use of it in real time. For example when exactly should I use transactions in real time. Let's suppose, I am writing code where I am doing some action on the click event of a link. Lets say, I am hitting the SQL connection to get some values. Should I use transaction there? If I am writing simple code, where I fetch values without using sql connection, should I use transactions there? What are the pros and cons of using Transactions. Getting theoritical knowledge is different, but I want to know the exact use of it. When to use when not to use. Can transactions be used in simple code? Any responses or links for even basic stuff about transactions are welcome.

推荐答案

我正在点击SQL连接以获取一些值.我应该使用有交易吗?

I am hitting the SQL connection to get some values. Should I use transaction there?

不,当您不更改数据库中的数据时,不需要使用事务.

No, there are not need to use transactions, when you are not alter data in database.

使用交易的优缺点.

What are the pros and cons of using Transactions.

正如您所说,您已经学到了很多文章,所以也许您已经弄清楚了使用交易的原因.看看所有这些与数据库有关.

As you said, you have learned various articles, So may be you have figure out the reason of using the transactions. Look all of these in concern of database.

事务处理系统使三层应用程序在创建可伸缩和强大的应用程序中的优势变得可行.无需明确为该体系结构开发就可以在单独的服务器之间分配组成应用程序的组件的能力,这是事务服务器处理的另一个优点.事务处理系统还确保事务是原子的,一致的,隔离的和持久的.这使开发人员不必显式地支持这些特性.

为什么我们需要交易处理?
交易处理的优势

可以用简单的代码使用交易吗?

Can transactions be used in simple code?

是的,您可以简单地使用ADO.Net用C#编写代码.(例如SQLTransaction类)

Yes, you can simply write code in C# using ADO.Net. (e.g. SQLTransaction class)

例如

SqlConnection db = new SqlConnection("connstringhere");
      SqlTransaction transaction;

      db.Open();
      transaction = db.BeginTransaction();
      try 
      {
         new SqlCommand("INSERT INTO TransactionDemo " +
            "(Text) VALUES ('Row1');", db, transaction)
            .ExecuteNonQuery();
         new SqlCommand("INSERT INTO TransactionDemo " +
            "(Text) VALUES ('Row2');", db, transaction)
            .ExecuteNonQuery();
         new SqlCommand("INSERT INTO CrashMeNow VALUES " +
            "('Die', 'Die', 'Die');", db, transaction)
            .ExecuteNonQuery();
         transaction.Commit();

参考:
使用ADO.NET执行事务
.NET 2.0交易模型

这篇关于.NET中使用C#进行的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆