asp.net創(chuàng)建事務的方法
更新時間:2016年12月19日 11:20:44 作者:冷戰(zhàn)
本篇文章主要對asp.net創(chuàng)建事務的方法進行實例介紹,具有很好的參考價值,需要的朋友一起來看下吧
1、建立List用于存放多條語句
/// <summary> /// 保存表單 /// </summary> /// <param name="context"></param> protected void save() { List<string> list = new List<string>(); 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、調(diào)用ExecuteTransaction方法,并返回返回值true為成功,false為失敗,語句并回滾
/// <summary> /// 執(zhí)行語句 /// </summary> /// <param name="list"></param> /// <returns></returns> private bool ExecuteTransaction(List<string> 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?中配置從xml轉(zhuǎn)向json方法示例詳解
這篇文章主要為大家介紹了.NET?中配置從xml轉(zhuǎn)向json方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11在?ASP.NET?Core?中為?gRPC?服務添加全局異常處理
這篇文章主要介紹了在?ASP.NET?Core?中為?gRPC?服務添加全局異常處理?,在?ASP.NET?Core?中使用?GRPC.ASPNETCore?工具包寫?gRPC?服務,想實現(xiàn)?gRPC?的異常全局攔截,下面一起來看看文中的詳細內(nèi)容吧2022-01-01使用Asp.net Mvc3 Razor視圖方式擴展JQuery UI Widgets方法介紹
jquery easyui grid或者extjs grid,jtable的代碼非常簡潔、對于grid功能要求不是很復雜的情況下,強烈推薦大家使用2012-11-11深入分析XmlSerializer對象的Xml序列化與反序列化的示例詳解
本篇文章是對XmlSerializer 對象的Xml序列化與反序列化的應用進行了詳細的分析介紹,需要的朋友參考下2013-05-05ASP.NET Web API教程 創(chuàng)建Admin視圖詳細介紹
現(xiàn)在我們轉(zhuǎn)入客戶端,并添加一個能夠使用從Admin控制器而來的數(shù)據(jù)的頁面。通過給控制器發(fā)送AJAX請求的方式,該頁面將允許用戶創(chuàng)建、編輯,或刪除產(chǎn)品2012-11-11水晶易表調(diào)用C#的WebService,返回數(shù)據(jù)集合的應用分析
本篇文章介紹了,水晶易表調(diào)用C#的WebService,返回數(shù)據(jù)集合的應用分析。需要的朋友參考下2013-04-04