网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

asp.net中怎么创建事务-创新互联

本篇文章为大家展示了asp.net中怎么创建事务,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联公司专注于道外网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供道外营销型网站建设,道外网站制作、道外网页设计、道外网站官网定制、小程序设计服务,打造道外网络公司原创品牌,更为您提供道外网站排名全网营销落地服务。

1、建立List用于存放多条语句

/// 
/// 保存表单
/// 
/// 
protected void save()
{
 List list = 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;
  }
 }
}

上述内容就是asp.net中怎么创建事务,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


分享名称:asp.net中怎么创建事务-创新互联
网页链接:http://bjjierui.cn/article/dsdecj.html

其他资讯