您现在的位置是:网站首页> 编程资料编程资料
asp.net创建事务的方法_ASP.NET_
2023-05-24
280人已围观
简介 asp.net创建事务的方法_ASP.NET_
1、建立List用于存放多条语句
////// 保存表单 /// /// protected void save() { Listlist = new List (); list.Add(string.Format("insert into picsone(model,idser,idflg,lmuser,lmdate,lmtime) values('{0}','{1}','{2}','{3}',{4},{5})", "T1002", "Y", "N", "U001", 20161103, 140025)); list.Add(string.Format("insert into picstwo(model,idser,idflg,lmuser,lmdate,lmtime) values('{0}','{1}','{2}','{3}',{4},{5})", "T1002", "Y", "N", "U001", 20161103, 140025)); bool bol = ExecuteTransaction(list); if (bol) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } }
2、调用ExecuteTransaction方法,并返回返回值true为成功,false为失败,语句并回滚
////// 执行语句 /// /// ///private bool ExecuteTransaction(List list) { using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["LocalConnectionString"].ToString())) { SqlCommand command = new SqlCommand(); SqlTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); command.Connection = connection; command.Transaction = transaction; for (int i = 0; i < list.Count; i++) { command.CommandText = list[i]; command.ExecuteNonQuery(); } transaction.Commit(); connection.Close(); return true; } catch { transaction.Rollback(); connection.Close(); return false; } } }
相关内容
- 详解.NET中使用Redis数据库_实用技巧_
- 详解开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)_实用技巧_
- 详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)_实用技巧_
- 详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)_实用技巧_
- 详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)_实用技巧_
- 详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)_实用技巧_
- 详解ASP.NET Core 在 JSON 文件中配置依赖注入_实用技巧_
- .NET中的repeater简介及分页效果_实用技巧_
- ASP.NET Core MVC压缩样式、脚本详解_实用技巧_
- win8/8.1系统安装.net framework 3.5出现0x800F0906代码错误的解决方法_实用技巧_
