使用SqlTransaction回滚事务
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回滚事务的更多相关文章
- SqlTransaction的解析
SqlTransaction的解析 2011-10-10 19:48 2757人阅读 评论(1) 收藏 举报 exceptionsql serverinsertcommandobjectstring ...
- c#传统SqlTransaction事务和TransactionScope事务
事务有很多种,看了一些关于事务的问题,这里做下笔记····· 事务时单个的工作单位.如果某一事务成功,则在该事务中进行的所有数据更改均会提交,成为数据库中永久的组成部分.若果事务遇到错误,则必须取消或 ...
- 如何判断事务是否完成,SqlTransaction
SqlConnection sconn = null; SqlCommand scmd = null; SqlTransaction strans = null; try { string sqlIn ...
- ASP.NET - 回滚事务
SqlConnection con =new SqlConnection(ConnectionDb.conStr);//获取数据库连接 con.Open();//打开连接 SqlTransaction ...
- 为什么SqlTransaction.Rollback会抛出SqlException(11,-2)(即SQL超时异常)
// System.Data.SqlClient.SqlTransaction public override void Rollback() { if (this.IsYukonPartia ...
- ErrorCode枚举类型返回错误码信息测试,手动抛出异常信息,在事务中根据错误码来回滚事务的思路。
ErrorCode.java 简单测试代码,具体应用思路:手动抛出异常信息,在事务中根据错误码来回滚事务的思路. public enum ErrorCode { //系统级 SUCCESS(" ...
- SQL Server 事务处理 回滚事务
--创建表: GO CREATE TABLE [dbo].[tb1]( [Id] [int] NOT NULL, [c1] [nvarchar](50) NULL, [c2] [datetime] N ...
- sql tran, c# SqlTransaction , TransactionScope 的用法
本节主要介绍Sql语句,SqlTransaction和TransactionScope这三种使用事务的方法. 本节的所有例子都在sql server 2008和vs 2008环境下运行通过 ...
- SQL Server 事务及回滚事务的几种方法
第一种: declare @iErrorCount int set@iErrorCount=0 begintran Tran1 insertinto t1(Id, c1) values( ...
随机推荐
- [工作记录] Android OpenGL ES: non-square texture - continue
previous: [工作记录] Android OpenGL ES 2.0: square texture not supported on some device recently I found ...
- IIS Express 一个网站配置多个 域名
在配置localhost和IP都可以访问: 方法1: applicationhost.config文件配置: <bindings> <binding protocol=& ...
- .NET设计模式(10):装饰模式(Decorator Pattern)(转)
概述 在软件系统中,有时候我们会使用继承来扩展对象的功能,但是由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性:并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多 ...
- Ubuntu下配置Docbook环境
1.准备环境 $sudo apt-get install xsltproc $sudo apt-get install docbook-xsl $sudo apt-get install docboo ...
- C/C++中内存区域划分大总结
C++作为一款C语言的升级版本,具有非常强大的功能.它不但能够支持各种程序设计风格,而且还具有C语言的所有功能.我们在这里为大家介绍的是其中一个比较重要的内容,C和C++内存区域的划分. 一. 在c中 ...
- Sqli-labs less 39
Less-39 和less-38的区别在于sql语句的不一样:SELECT * FROM users WHERE id=$id LIMIT 0,1 也就是数字型注入,我们可以构造以下的payload: ...
- c# 发送消息到Email
/// <summary> /// 发送消息到Email /// </summary> /// <param name=&quo ...
- -高级Javascript编程学习笔记----Javascript编程及架构设计最应该注意的基本点
最小全局变量 JavaScript通过函数管理作用域.在函数内部生命的变量只在这个函数内部,别的地方不可用.全局变量是指在函数外或是未声明直接简单使用的.每个Javascipt环境有一个全局对象,当你 ...
- Linux的别名使用
直接定义别名 编辑当前用户下的.bashrc 文件: vim ~/.bashrc 添加别名为 lmysql 的命令语句 : alias lmysql='mysql -uroot -p -Dtest ...
- Lock wait timeout exceeded; try restarting transaction
What gives this away is the word transaction. It is evident by the statement that the query was atte ...