https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx

private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open(); SqlCommand command = connection.CreateCommand();
SqlTransaction transaction; // Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction"); // Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction; try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery(); // Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message); // Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}

使用SqlTransaction回滚事务的更多相关文章

  1. SqlTransaction的解析

    SqlTransaction的解析 2011-10-10 19:48 2757人阅读 评论(1) 收藏 举报 exceptionsql serverinsertcommandobjectstring ...

  2. c#传统SqlTransaction事务和TransactionScope事务

    事务有很多种,看了一些关于事务的问题,这里做下笔记····· 事务时单个的工作单位.如果某一事务成功,则在该事务中进行的所有数据更改均会提交,成为数据库中永久的组成部分.若果事务遇到错误,则必须取消或 ...

  3. 如何判断事务是否完成,SqlTransaction

    SqlConnection sconn = null; SqlCommand scmd = null; SqlTransaction strans = null; try { string sqlIn ...

  4. ASP.NET - 回滚事务

    SqlConnection con =new SqlConnection(ConnectionDb.conStr);//获取数据库连接 con.Open();//打开连接 SqlTransaction ...

  5. 为什么SqlTransaction.Rollback会抛出SqlException(11,-2)(即SQL超时异常)

    // System.Data.SqlClient.SqlTransaction public override void Rollback() {     if (this.IsYukonPartia ...

  6. ErrorCode枚举类型返回错误码信息测试,手动抛出异常信息,在事务中根据错误码来回滚事务的思路。

    ErrorCode.java 简单测试代码,具体应用思路:手动抛出异常信息,在事务中根据错误码来回滚事务的思路. public enum ErrorCode { //系统级 SUCCESS(" ...

  7. SQL Server 事务处理 回滚事务

    --创建表: GO CREATE TABLE [dbo].[tb1]( [Id] [int] NOT NULL, [c1] [nvarchar](50) NULL, [c2] [datetime] N ...

  8. sql tran, c# SqlTransaction , TransactionScope 的用法

    本节主要介绍Sql语句,SqlTransaction和TransactionScope这三种使用事务的方法.       本节的所有例子都在sql server 2008和vs 2008环境下运行通过 ...

  9. SQL Server 事务及回滚事务的几种方法

    第一种: declare   @iErrorCount   int set@iErrorCount=0 begintran Tran1    insertinto t1(Id, c1) values( ...

随机推荐

  1. 实现IDisposable接口的模式

    代码: public class Class2 : IDisposable { ~Class2() { Dispose(false); } public void Dispose() { Dispos ...

  2. [工作记录] Android OpenGL ES: non-square texture - continue

    previous: [工作记录] Android OpenGL ES 2.0: square texture not supported on some device recently I found ...

  3. vml 在IE8 不显示的问题, Group不能用等问题.

    IE8 不显示的问题: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> ...

  4. HTTP常见返回代码(HTTP Status codes)的分类和含义

    HTTP错误主要分成三类:用户设备问题.Web服务器问题和连接问题.当客户端向Web服务器发送一个HTTP请求时,服务器都会返回一个响应代码.而这些响应代码主要分成五类. HTTP状态码中定义了5大类 ...

  5. zero to one:创业秘籍并不存在,因为任何创新都是新颖独特的,任何权威都不可能具体规定如何创新

    彼得·蒂尔(Peter Thiel)的新作<从0到1>从预售开始就占据美国亚马逊排行榜第一名的位置,被一批创业家和企业家评为“迄今为止最好的商业书”.这是一本关于如何创建创新公司的书,主要 ...

  6. Sqli-labs less 54

    第四部分/page-4 Challenges Less-54 此系列主要是一个进阶的学习,将前面学到的知识进行更深次的运用.这一关我们主要考察的依旧是字符型注入,但是只能尝试十次.所以需要在尝试的时候 ...

  7. SEO优化的黑帽手法是否值得使用?

    PR劫持 可能很多人也会听到说,什么网站权重越高越好,这也就是后面越来越多人都对谷歌的PR的宣传看的很重,自建站的都追求PR值,权重越高代表这个网站越受信任. 比如一个新站PR值为0,一个老站PR为6 ...

  8. MEAN实践——LAMP的新时代替代方案(下)

    在本系列文章的第一部分旨在介绍一些应用程序的基础技术细节和如何进行数据建模,而这个部分文章将着手建立验证应用程序行为的测试,并会指出如何启动和运行应用程序. 首先,编写测试 首先定义一些小型配置库.文 ...

  9. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  10. JAVA容器

    JAVA容器 一.容器体系结构 java.util 二.迭代器Iterator<E> 迭代器是一种设计模式,可以遍历并选择序列中的对象,而开发人员并不需要了解该序列的底层结构.迭代器通常被 ...