[C#]分享一个以前的项目使用的DataBaseAccess类(11)

/// <summary>
        /// execute SQL script,Keep SqlTransaction 。
        /// </summary>
        /// <param>save sql command and sql parameter</param>
        public void ExecuteSqlTran(Dictionary<string ,SqlParameter []> objSqlList)
        {
            conn.Open();
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                try
                {
                    SqlCommand sqlcmd = new SqlCommand();
                    //circulation                
                    foreach (KeyValuePair<string, SqlParameter[]> kvp in objSqlList)
                    {
                        //the key value is by|Division ,Serial number|sql script
                        string[] tmp = kvp.Key.ToString().Split(new char[] { '|' });                       
                        string cmdText = tmp[1];
                        //get SqlParameter value
                        SqlParameter[] sqlParms = kvp.Value;
                        if (sqlParms!=null)
                            sqlcmd = SQLHelper.CreateCommand(cmdText, sqlParms, this.conn);
                        sqlcmd.Transaction = trans;
                        int val = sqlcmd.ExecuteNonQuery();
                        //clear SqlParameter
                        sqlcmd.Parameters.Clear();
                    }
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    throw;
                }
                finally
                {                     
                    this.conn.Close();
                }
            }

}

#endregion

#endregion

#region other

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjwyj.html