asp.net创建事务的方法
1、建立List用于存放多条语句
///<summary>
///保存表单
///</summary>
///<paramname="context"></param>
protectedvoidsave()
{
List<string>list=newList<string>();
list.Add(string.Format("insertintopicsone(model,idser,idflg,lmuser,lmdate,lmtime)values('{0}','{1}','{2}','{3}',{4},{5})","T1002","Y","N","U001",20161103,140025));
list.Add(string.Format("insertintopicstwo(model,idser,idflg,lmuser,lmdate,lmtime)values('{0}','{1}','{2}','{3}',{4},{5})","T1002","Y","N","U001",20161103,140025));
boolbol=ExecuteTransaction(list);
if(bol)
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败!");
}
}
2、调用ExecuteTransaction方法,并返回返回值true为成功,false为失败,语句并回滚
///<summary>
///执行语句
///</summary>
///<paramname="list"></param>
///<returns></returns>
privateboolExecuteTransaction(List<string>list)
{
using(SqlConnectionconnection=newSqlConnection(ConfigurationManager.AppSettings["LocalConnectionString"].ToString()))
{
SqlCommandcommand=newSqlCommand();
SqlTransactiontransaction=null;
try
{
connection.Open();
transaction=connection.BeginTransaction();
command.Connection=connection;
command.Transaction=transaction;
for(inti=0;i<list.Count;i++)
{
command.CommandText=list[i];
command.ExecuteNonQuery();
}
transaction.Commit();
connection.Close();
returntrue;
}
catch
{
transaction.Rollback();
connection.Close();
returnfalse;
}
}
}
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短