C#使用事务
2015-12-07 15:05
465 查看
using (SqlConnection conn = new SqlConnection(Connstring) { conn .Open(); // 开启一个事务 SqlTransaction sqlTran = conn .BeginTransaction(); // Enlist a command in the current transaction. SqlCommand command = conn .CreateCommand(); //begin transaction command.Transaction = sqlTran; try { // 执行sql语句 command.CommandText ="xxxxx"; command.ExecuteNonQuery(); command.CommandText ="yyyyyy"; command.ExecuteNonQuery(); // 提交 sqlTran.Commit(); } catch (Exception ex) { // 执行失败回滚操作. lblMsg.Text = ex.Message; try { // 回滚. sqlTran.Rollback(); } catch (Exception exRollback) { // 异常 lblMsg.Text = exRollback.Message; } } }
相关文章推荐
- C#邮箱发送验证码
- C#实现简单聊天程序的方法
- C#邮箱发送
- C#类型转换总结(三)各类型转化实例
- C# 八进制数值字符串转换成ASCII 字符串相互转换
- C# 操作自定义config文件
- c#生成时间戳
- C# 函数中的out型参数
- C#模拟windows账户登录,并复制文件代码
- c#之时间戳与DateTime的相互转换
- c#读取excel的xls和xlsx文件的方法
- C#.net 数据操作--excel 操作类
- C#中的扩展方法学习总结
- C# AOP框架入门
- 基于C#实现的高斯正反算公式实现的国家大地2000坐标转换功能
- pp看书笔记---C#高级编程第九版 第四章 【继承】
- c#面向对象
- C#中数组、ArrayList和List三者的区别
- C#——字符串总结
- 【C#基础】基础总结