使用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( ...
随机推荐
- HDU 2196 求树上所有点能到达的最远距离
其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...
- 【BZOJ】【1053】【HAOI2007】反素数ant
搜索 经典搜索题目(其实是蒟蒻只会搜……vfleaking好像有更优秀的做法?) 枚举质数的幂,其实深度没多大……因为$2^32$就超过N了……而且质数不能取的太大,所以不会爆…… /******** ...
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 常用的Python字符串常量
下面是一些常用的Python字符串常量string.digits:包含0-9的字符串string.letters:包含所有大小写字母的字符串 string.lowercase:所有小写字母string ...
- CSS自定义select下拉选择框(不用其他标签模拟)
今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...
- SWF Web播放器
<HTML> <HEAD> <!-- saved from url=(0013)about:internet --> <TITLE> Untitled. ...
- 教你如何利用xml格式的sitemap文件做好SEO
教你如何利用xml格式的sitemap文件做好SEO 浏览: | 更新:-- : 一般的网站中都有网站地图文件,它有HTML格式与XML格式,网站地图可以帮助搜索引擎抓取.帮助用户找到自己所需要的内容 ...
- PHP一些函数
函数不定参数: func_num_args // 获得参数个数 func_get_args // 获得参数数组 call_user_func('function_name', $parameter) ...
- Andoid自动判断输入是电话,网址或者Email的方法----Linkify的应用!
本节要讲的是,当我们在一个EditText输入电话或者网址还是Email的时候,让Android自动判断,当我们输入的是电话,我们点击输入内容将调用打电话程序,当我们输入是网址点击将打开浏览器程序.而 ...
- hdu 2999 Stone Game, Why are you always there? 博弈论
SG函数应用!! 代码如下: #include<cstdio> #include<cstring> #include<iostream> #include<c ...