如何判断事务是否完成,SqlTransaction
2024-09-11 15:54:08
SqlConnection sconn = null;
SqlCommand scmd = null;
SqlTransaction strans = null;
try
{
string sqlInsert = string.Format(@"insert into MyName (myid,myfname,mymname,mylname) values
('{0}','c','c','c')", Guid.NewGuid().ToString());
sconn = new SqlConnection(dbConnStr);
sconn.Open();
scmd = new SqlCommand(sqlInsert, sconn);
strans = sconn.BeginTransaction();
scmd.Transaction = strans; scmd.ExecuteNonQuery(); strans.Commit(); int[] myi = new int[] { };
int myr = myi[];
}
catch (Exception exp)
{
if (strans != null && strans.Connection != null)
strans.Rollback(); MessageBox.Show(exp.Message);
}
finally
{
if (sconn != null)
sconn.Close();
}
如果事务完成后,还有其它代码引发异常,这时就不能去回滚事务了。事务一旦提交,它的Connection就为NULL了,可以通过if (strans != null && strans.Connection != null) 来判断。
最新文章
- List集合去重的一种方法
- mysql中更新或者删除语句中子语句不能操作同一个表You can't specify target table 'test' for update in FROM clause
- ADC/DAC的一些参数
- eclipse创建web项目
- B - Kefa and Company
- 【剑指offer 面试题7】用两个栈实现队列
- 配置VirtualBox Linux系统与Windows共享文件
- POJ3071:Football(概率DP)
- python3.6+linux服务器+django1.11连接MYSQL数据库
- Property 'id' not found on type java.lang.String
- 【安富莱原创开源应用第3期】花式玩转网络摄像头之VNC远程桌面版本,稳定运行2年不死机
- Neutron flat network 学习
- redis 远程操作命令
- hadoop学习笔记(五):java api 操作hdfs
- Intellij-怎么避免import.*包,以及import包顺序问题
- November 15th 2016 Week 47th Tuesday
- postman--实现接口自动化测试
- Redis系列一 Redis安装
- 【Mac + Appium + Python3.6学习(六)】之安装Android模拟器(Genymotion)并运行模拟器进行自动化
- 如何获取.properties配置文件
热门文章
- VGA
- sgu 125 Shtirlits dfs 难度:0
- zoj3888
- L242
- koa 微信小程序 项目
- 显式(静态)调用: LIB + DLL + .H
- Java面试通关要点汇总整理【终极版】
- https页面证书验证、加密过程简介
- 第八章 Redis数据库结构与读写原理
- com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 问题解决方法