A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-8-13 10:59  /  1059 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.Data.SqlClient;
  3. namespace ExecuteSqlTran
  4. {
  5.     class Program
  6.     {
  7.         class Result<T>
  8.         {
  9.             public T data;
  10.             public string Message;
  11.             public bool Success;
  12.             public string StackTrace;
  13.         }

  14.         struct ExecuteableUnit
  15.         {
  16.             public string SQL;
  17.             public SqlParameter[] param;
  18.         }

  19.         /// <summary>
  20.         /// 执行多条SQL语句,实现数据库事务。
  21.         /// </summary>
  22.         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  23.         private static Result<int> ExecuteSqlTransaction(params ExecuteableUnit[] executeableUnits)
  24.         {
  25.             using (SqlConnection connection = new SqlConnection(""))
  26.             {
  27.                 connection.Open();
  28.                 SqlCommand command = connection.CreateCommand();
  29.                 SqlTransaction transaction = connection.BeginTransaction();
  30.                 command.Connection = connection;
  31.                 command.Transaction = transaction;
  32.                 int result = 0;
  33.                 try
  34.                 {
  35.                     foreach(ExecuteableUnit exeUnit in executeableUnits)
  36.                     {
  37.                         command.CommandText = exeUnit.SQL;
  38.                         if(exeUnit.param.GetLength(1) > 0)
  39.                         {
  40.                             foreach(SqlParameter p in exeUnit.param)
  41.                                 command.Parameters.Add(p);
  42.                         }
  43.                         result += command.ExecuteNonQuery();
  44.                     }
  45.                     transaction.Commit();
  46.                 }
  47.                 catch (Exception ex)
  48.                 {
  49.                     // Attempt to roll back the transaction.
  50.                     try
  51.                     {
  52.                         transaction.Rollback();
  53.                     }
  54.                     catch (Exception ex2)
  55.                     {
  56.                         return new Result<int>()
  57.                         {
  58.                             Success = false, Message = ex2.Message, StackTrace = ex2.StackTrace
  59.                         };
  60.                     }
  61.                     return new Result<int>()
  62.                     {
  63.                         Success = false, Message = ex.Message, StackTrace = ex.StackTrace
  64.                     };
  65.                 }
  66.                 finally
  67.                 {
  68.                     // Attempt to roll back the transaction.
  69.                     try
  70.                     {
  71.                         connection.Close();
  72.                     }
  73.                     catch (Exception ex)
  74.                     {
  75.                     }
  76.                 }
  77.                 return new Result<int>()
  78.                 {
  79.                     Success = true, data = result
  80.                 };
  81.             }
  82.         }

  83.         public static void Main(string[] args)
  84.         {
  85.         }
  86.     }
  87. }
复制代码


1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马