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) 来判断。

最新文章

  1. List集合去重的一种方法
  2. mysql中更新或者删除语句中子语句不能操作同一个表You can't specify target table 'test' for update in FROM clause
  3. ADC/DAC的一些参数
  4. eclipse创建web项目
  5. B - Kefa and Company
  6. 【剑指offer 面试题7】用两个栈实现队列
  7. 配置VirtualBox Linux系统与Windows共享文件
  8. POJ3071:Football(概率DP)
  9. python3.6+linux服务器+django1.11连接MYSQL数据库
  10. Property 'id' not found on type java.lang.String
  11. 【安富莱原创开源应用第3期】花式玩转网络摄像头之VNC远程桌面版本,稳定运行2年不死机
  12. Neutron flat network 学习
  13. redis 远程操作命令
  14. hadoop学习笔记(五):java api 操作hdfs
  15. Intellij-怎么避免import.*包,以及import包顺序问题
  16. November 15th 2016 Week 47th Tuesday
  17. postman--实现接口自动化测试
  18. Redis系列一 Redis安装
  19. 【Mac + Appium + Python3.6学习(六)】之安装Android模拟器(Genymotion)并运行模拟器进行自动化
  20. 如何获取.properties配置文件

热门文章

  1. VGA
  2. sgu 125 Shtirlits dfs 难度:0
  3. zoj3888
  4. L242
  5. koa 微信小程序 项目
  6. 显式(静态)调用: LIB + DLL + .H
  7. Java面试通关要点汇总整理【终极版】
  8. https页面证书验证、加密过程简介
  9. 第八章 Redis数据库结构与读写原理
  10. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 问题解决方法