国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#實現數據庫事務示例分享

2020-01-24 02:52:33
字體:
來源:轉載
供稿:網友


復制代碼 代碼如下:

using System;
using System.Data.SqlClient;

namespace ExecuteSqlTran
{
    class Program
    {
        class Result<T>
        {
            public T data;
            public string Message;
            public bool Success;
            public string StackTrace;
        }

        struct ExecuteableUnit
        {
            public string SQL;
            public SqlParameter[] param;
        }

        /// <summary>
        /// 執行多條SQL語句,實現數據庫事務。
        /// </summary>
        /// <param name="SQLStringList">SQL語句的哈希表(key為sql語句,value是該語句的SqlParameter[])</param>
        private static Result<int> ExecuteSqlTransaction(params ExecuteableUnit[] executeableUnits)
        {
            using (SqlConnection connection = new SqlConnection(""))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                SqlTransaction transaction = connection.BeginTransaction();
                command.Connection = connection;
                command.Transaction = transaction;
                int result = 0;
                try
                {
                    foreach(ExecuteableUnit exeUnit in executeableUnits)
                    {
                        command.CommandText = exeUnit.SQL;
                        if(exeUnit.param.GetLength(1) > 0)
                        {
                            foreach(SqlParameter p in exeUnit.param)
                                command.Parameters.Add(p);
                        }
                        result += command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    // Attempt to roll back the transaction.
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        return new Result<int>()
                        {
                            Success = false, Message = ex2.Message, StackTrace = ex2.StackTrace
                        };
                    }
                    return new Result<int>()
                    {
                        Success = false, Message = ex.Message, StackTrace = ex.StackTrace
                    };
                }
                finally
                {
                    // Attempt to roll back the transaction.
                    try
                    {
                  connection.Close();
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return new Result<int>()
                {
                    Success = true, data = result
                };
            }
        }

        public static void Main(string[] args)
        {
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 济阳县| 灵台县| 阳江市| 营口市| 米泉市| 石台县| 威海市| 萍乡市| 大荔县| 增城市| 临高县| 绵竹市| 中江县| 漳州市| 无极县| 五指山市| 宁波市| 通许县| 宁蒗| 博湖县| 丰镇市| 乌什县| 镇雄县| 开化县| 蓬溪县| 波密县| 新干县| 宾阳县| 额敏县| 临泽县| 兴宁市| 昭平县| 上犹县| 彩票| 自贡市| 正镶白旗| 成都市| 内黄县| 上林县| 化州市| 上林县|