asp.net創(chuàng)建事務(wù)的方法
1、建立List用于存放多條語(yǔ)句
/// <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為失敗,語(yǔ)句并回滾
/// <summary>
/// 執(zhí)行語(yǔ)句
/// </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;
}
}
}
相關(guān)文章
.NET?中配置從xml轉(zhuǎn)向json方法示例詳解
這篇文章主要為大家介紹了.NET?中配置從xml轉(zhuǎn)向json方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
在?ASP.NET?Core?中為?gRPC?服務(wù)添加全局異常處理
這篇文章主要介紹了在?ASP.NET?Core?中為?gRPC?服務(wù)添加全局異常處理?,在?ASP.NET?Core?中使用?GRPC.ASPNETCore?工具包寫(xiě)?gRPC?服務(wù),想實(shí)現(xiàn)?gRPC?的異常全局?jǐn)r截,下面一起來(lái)看看文中的詳細(xì)內(nèi)容吧2022-01-01
使用Asp.net Mvc3 Razor視圖方式擴(kuò)展JQuery UI Widgets方法介紹
jquery easyui grid或者extjs grid,jtable的代碼非常簡(jiǎn)潔、對(duì)于grid功能要求不是很復(fù)雜的情況下,強(qiáng)烈推薦大家使用2012-11-11
深入分析XmlSerializer對(duì)象的Xml序列化與反序列化的示例詳解
本篇文章是對(duì)XmlSerializer 對(duì)象的Xml序列化與反序列化的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
ASP.NET Web API教程 創(chuàng)建Admin視圖詳細(xì)介紹
現(xiàn)在我們轉(zhuǎn)入客戶(hù)端,并添加一個(gè)能夠使用從Admin控制器而來(lái)的數(shù)據(jù)的頁(yè)面。通過(guò)給控制器發(fā)送AJAX請(qǐng)求的方式,該頁(yè)面將允許用戶(hù)創(chuàng)建、編輯,或刪除產(chǎn)品2012-11-11
水晶易表調(diào)用C#的WebService,返回?cái)?shù)據(jù)集合的應(yīng)用分析
本篇文章介紹了,水晶易表調(diào)用C#的WebService,返回?cái)?shù)據(jù)集合的應(yīng)用分析。需要的朋友參考下2013-04-04
利用ASP.NET技術(shù)動(dòng)態(tài)生成HTML頁(yè)面
利用ASP.NET技術(shù)動(dòng)態(tài)生成HTML頁(yè)面...2006-07-07
.Net執(zhí)行SQL存儲(chǔ)過(guò)程之易用輕量工具詳解
這篇文章主要為大家介紹了.Net執(zhí)行SQL存儲(chǔ)過(guò)程之易用輕量工具詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

