示例#1
0
        public static void ExecInTrans(DALExecuteArgs args, ExecInTransDelegate execDelegate)
        {
            if (args.DbConnection != null && args.DbTrans != null)
            {
                execDelegate(args.DbConnection, args.DbTrans);
            }
            else
            {
                string dbName = args.DBName;
                //using( DbConnection conn = Provider.CreateConnection( dbName ) )
                using (DbConnection conn = Provider.CreateConnection())
                {
                    try
                    {
                        conn.Open();

                        IsolationLevel level;
                        //if( TS.DW.Setting.SettingDW.TransMode == 0 )
                        if (iTransMode == 0)
                        {
                            level = IsolationLevel.ReadCommitted;
                        }
                        else
                        {
                            level = IsolationLevel.ReadUncommitted;
                        }
                        using (DbTransaction trans = conn.BeginTransaction(level))
                        {
                            using (DbCommand cmd = Provider.CreateCommand())
                            {
                                try
                                {
                                    execDelegate(conn, trans);

                                    trans.Commit();
                                }
                                catch (Exception ex)
                                {
                                    try
                                    {
                                        trans.Rollback();
                                    }
                                    catch
                                    {
                                    }
                                    throw ex;
                                }
                            }
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
示例#2
0
 public static DataTable ExecuteQuery(DALExecuteArgs args, string cmdText, bool IsNeedTran)
 {
     //是否需要事务
     if (IsNeedTran)
     {
         return(ExecuteQuery(args, cmdText, null));
     }
     else
     {
         return(ExecuteQuery(args, cmdText, enQueryTransType.Non, null));
     }
 }
示例#3
0
 public static DataSet ExecuteQuery4DataSet(DALExecuteArgs args, string cmdText, enQueryTransType transType, params DbParameter[] cmdParms)
 {
     return(ExecuteQuery4DataSet(args, CommandType.Text, cmdText, transType, cmdParms));
 }
示例#4
0
 //public static DataSet ExecuteQuery4DataSet( DALExecuteArgs args, string cmdText, enQueryTransType transType )
 //{
 //    return ExecuteQuery4DataSet( args, cmdText, null, transType );
 //}
 public static DataSet ExecuteQuery4DataSet(DALExecuteArgs args, string cmdText)
 {
     return(ExecuteQuery4DataSet(args, cmdText, enQueryTransType.Non, null));
 }
示例#5
0
        //private static DataSet ExecuteQuery4DataSet( DALExecuteArgs args, CommandType cmdType, string cmdText, TSDbParameterCollection cmdParms, enQueryTransType transType )
        private static DataSet ExecuteQuery4DataSet(DALExecuteArgs args, CommandType cmdType, string cmdText, enQueryTransType transType, params DbParameter[] cmdParms)
        {
            DataSet dsResult = null;

            if (args.DbConnection == null)
            {
                if (transType != enQueryTransType.Non)
                {
                    #region -- 没有传入 DbConnection,需要事务 --

                    //using( DbConnection conn = Provider.CreateConnection( args.DBName ) )
                    using (DbConnection conn = Provider.CreateConnection())
                    {
                        conn.Open();

                        IsolationLevel level;
                        if (transType == enQueryTransType.Snapshot && Provider.SnapshotSupported(args.DBName))
                        {
                            level = IsolationLevel.Snapshot;
                        }
                        else
                        {
                            level = IsolationLevel.ReadCommitted;
                        }

                        using (DbTransaction trans = conn.BeginTransaction(level))
                        {
                            using (DbCommand cmd = Provider.CreateCommand())
                            {
                                try
                                {
                                    // 准备参数
                                    PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);

                                    // 执行
                                    dsResult = new DataSet();
                                    DbDataAdapter adapter = Provider.CreateDataAdapter();
                                    adapter.SelectCommand = cmd;
                                    adapter.Fill(dsResult);

                                    // 提交
                                    trans.Commit();

                                    //// 取输出参数值
                                    //GetParameterOut( cmdParms, cmd.Parameters );

                                    return(dsResult);
                                }
                                catch (Exception ex)
                                {
                                    try
                                    {
                                        trans.Rollback();
                                    }
                                    catch
                                    {
                                    }
                                    throw ex;
                                }
                            }
                        }
                    }

                    #endregion -- 没有传入 DbConnection,需要事务 --
                }
                else
                {
                    #region -- 没有传入 DbConnection,不需要事务 --

                    //using( DbConnection conn = Provider.CreateConnection( args.DBName ) )
                    using (DbConnection conn = Provider.CreateConnection())
                    {
                        using (DbCommand cmd = Provider.CreateCommand())
                        {
                            // 准备参数
                            PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);

                            // 执行
                            dsResult = new DataSet();
                            DbDataAdapter adapter = Provider.CreateDataAdapter();
                            adapter.SelectCommand = cmd;
                            adapter.Fill(dsResult);

                            //// 取输出参数值
                            //GetParameterOut( cmdParms, cmd.Parameters );

                            return(dsResult);
                        }
                    }

                    #endregion -- 没有传入 DbConnection,不需要事务 --
                }
            }
            else if (args.DbTrans != null)
            {
                #region -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 --

                using (DbCommand cmd = Provider.CreateCommand())
                {
                    // 准备参数
                    PrepareCommand(cmd, args.DbConnection, args.DbTrans, cmdType, cmdText, cmdParms);

                    // 执行
                    dsResult = new DataSet();
                    DbDataAdapter adapter = Provider.CreateDataAdapter();
                    adapter.SelectCommand = cmd;
                    adapter.Fill(dsResult);

                    //// 取输出参数值
                    //GetParameterOut( cmdParms, cmd.Parameters );

                    return(dsResult);
                }

                #endregion -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 --
            }
            else if (transType != enQueryTransType.Non)
            {
                #region -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 --

                throw new Exception("当需要事务执行时,如果传入 DbConnection,必须同时传入 DbTransaction。");

                #endregion -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 --
            }
            else
            {
                #region -- 传入 DbConnection,没有传入 DbTrans,不需要事务 --

                using (DbCommand cmd = Provider.CreateCommand())
                {
                    // 准备参数
                    PrepareCommand(cmd, args.DbConnection, null, cmdType, cmdText, cmdParms);

                    // 执行
                    dsResult = new DataSet();
                    DbDataAdapter adapter = Provider.CreateDataAdapter();
                    adapter.SelectCommand = cmd;
                    adapter.Fill(dsResult);

                    //// 取输出参数值
                    //GetParameterOut( cmdParms, cmd.Parameters );

                    return(dsResult);
                }

                #endregion -- 传入 DbConnection,没有传入 DbTrans,不需要事务 --
            }
        }
示例#6
0
 //public static DataTable ExecuteQuery( DALExecuteArgs args, string cmdText, TSDbParameterCollection cmdParms )
 public static DataTable ExecuteQuery(DALExecuteArgs args, string cmdText, params DbParameter[] cmdParms)
 {
     return(ExecuteQuery(args, CommandType.Text, cmdText, enQueryTransType.ReadCommitted, cmdParms));
 }
示例#7
0
 public static DataTable ExecuteQuery(DALExecuteArgs args, string cmdText)
 {
     //return ExecuteQuery(args, cmdText, null);
     return(ExecuteQuery(args, cmdText, enQueryTransType.Non, null));
 }
示例#8
0
 public static DataTable ExecuteQuery(DALExecuteArgs args, string cmdText, enQueryTransType transType)
 {
     return(ExecuteQuery(args, cmdText, transType, null));
 }
示例#9
0
        private static DataTable ExecuteQuery(DALExecuteArgs args, CommandType cmdType, string cmdText, enQueryTransType transType, params DbParameter[] cmdParms)
        {
            DataTable dtResult = null;

            if (args.DbConnection == null)
            {
                if (transType != enQueryTransType.Non)
                {
                    #region -- 没有传入 DbConnection,需要事务 --

                    //using( DbConnection conn = Provider.CreateConnection( args.DBName ) )
                    using (DbConnection conn = Provider.CreateConnection())
                    {
                        try
                        {
                            conn.Open();

                            IsolationLevel level;
                            //if( TS.DW.Setting.SettingDW.TransMode == 0 )
                            if (iTransMode == 0)
                            {
                                if (transType == enQueryTransType.ReadUncommitted)
                                {
                                    level = IsolationLevel.ReadUncommitted;
                                }
                                else if (transType == enQueryTransType.Snapshot && Provider.SnapshotSupported(args.DBName))
                                {
                                    level = IsolationLevel.Snapshot;
                                }
                                else
                                {
                                    level = IsolationLevel.ReadCommitted;
                                }
                            }
                            else
                            {
                                level = IsolationLevel.ReadUncommitted;
                            }

                            using (DbTransaction trans = conn.BeginTransaction(level))
                            {
                                using (DbCommand cmd = Provider.CreateCommand())
                                {
                                    try
                                    {
                                        // 准备参数
                                        PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);

                                        // 执行
                                        dtResult = new DataTable("Reuslt");
                                        DbDataAdapter adapter = Provider.CreateDataAdapter();
                                        adapter.SelectCommand = cmd;
                                        adapter.Fill(dtResult);

                                        // 提交
                                        trans.Commit();

                                        //// 取输出参数值
                                        //GetParameterOut( cmdParms, cmd.Parameters );

                                        return(dtResult);
                                    }
                                    catch (Exception ex)
                                    {
                                        try
                                        {
                                            trans.Rollback();
                                        }
                                        catch
                                        {
                                        }
                                        throw ex;
                                    }
                                }
                            }
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }

                    #endregion -- 没有传入 DbConnection,需要事务 --
                }
                else
                {
                    #region -- 没有传入 DbConnection,不需要事务 --

                    //using( DbConnection conn = Provider.CreateConnection( args.DBName ) )
                    using (DbConnection conn = Provider.CreateConnection())
                    {
                        try
                        {
                            using (DbCommand cmd = Provider.CreateCommand())
                            {
                                if (cmdType == CommandType.Text)
                                {
                                    cmdText = "set transaction isolation level read uncommitted;" + Environment.NewLine + cmdText;
                                }
                                // 准备参数
                                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);

                                // 执行
                                dtResult = new DataTable("Reuslt");
                                DbDataAdapter adapter = Provider.CreateDataAdapter();
                                adapter.SelectCommand = cmd;
                                adapter.Fill(dtResult);

                                //// 取输出参数值
                                //GetParameterOut( cmdParms, cmd.Parameters );

                                return(dtResult);
                            }
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }

                    #endregion -- 没有传入 DbConnection,不需要事务 --
                }
            }
            else if (args.DbTrans != null)
            {
                #region -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 --

                using (DbCommand cmd = Provider.CreateCommand())
                {
                    // 准备参数
                    PrepareCommand(cmd, args.DbConnection, args.DbTrans, cmdType, cmdText, cmdParms);

                    // 执行
                    dtResult = new DataTable("Reuslt");
                    DbDataAdapter adapter = Provider.CreateDataAdapter();
                    adapter.SelectCommand = cmd;
                    adapter.Fill(dtResult);

                    //// 取输出参数值
                    //GetParameterOut( cmdParms, cmd.Parameters );

                    return(dtResult);
                }

                #endregion -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 --
            }
            else if (transType != enQueryTransType.Non)
            {
                #region -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 --

                //throw new Exception( "当需要事务执行时,如果传入 DbConnection,必须同时传入 DbTransaction。" );
                IsolationLevel level;
                //if( TS.DW.Setting.SettingDW.TransMode == 0 )
                if (iTransMode == 0)
                {
                    if (transType == enQueryTransType.ReadUncommitted)
                    {
                        level = IsolationLevel.ReadUncommitted;
                    }
                    else if (transType == enQueryTransType.Snapshot && Provider.SnapshotSupported(args.DBName))
                    {
                        level = IsolationLevel.Snapshot;
                    }
                    else
                    {
                        level = IsolationLevel.ReadCommitted;
                    }
                }
                else
                {
                    level = IsolationLevel.ReadUncommitted;
                }

                using (DbTransaction trans = args.DbConnection.BeginTransaction(level))
                {
                    using (DbCommand cmd = Provider.CreateCommand())
                    {
                        try
                        {
                            // 准备参数
                            PrepareCommand(cmd, args.DbConnection, trans, cmdType, cmdText, cmdParms);

                            // 执行
                            dtResult = new DataTable("Reuslt");
                            DbDataAdapter adapter = Provider.CreateDataAdapter();
                            adapter.SelectCommand = cmd;
                            adapter.Fill(dtResult);

                            // 提交
                            trans.Commit();

                            //// 取输出参数值
                            //GetParameterOut( cmdParms, cmd.Parameters );

                            return(dtResult);
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch
                            {
                            }
                            throw ex;
                        }
                    }
                }
                #endregion -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 --
            }
            else
            {
                #region -- 传入 DbConnection,没有传入 DbTrans,不需要事务 --

                using (DbCommand cmd = Provider.CreateCommand())
                {
                    if (cmdType == CommandType.Text)
                    {
                        cmdText = "set transaction isolation level read uncommitted;" + Environment.NewLine + cmdText;
                    }
                    // 准备参数
                    PrepareCommand(cmd, args.DbConnection, null, cmdType, cmdText, cmdParms);

                    // 执行
                    dtResult = new DataTable("Reuslt");
                    DbDataAdapter adapter = Provider.CreateDataAdapter();
                    adapter.SelectCommand = cmd;
                    adapter.Fill(dtResult);

                    //// 取输出参数值
                    //GetParameterOut( cmdParms, cmd.Parameters );

                    return(dtResult);
                }

                #endregion -- 传入 DbConnection,没有传入 DbTrans,不需要事务 --
            }
        }
示例#10
0
        //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
        //  public static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        #region -- ExecuteNonQuery --

        //public static int ExecuteNonQuery( DALExecuteArgs args, CommandType cmdType, string cmdText, TSDbParameterCollection cmdParms, bool bNeedTrans )
        public static int ExecuteNonQuery(DALExecuteArgs args, CommandType cmdType, string cmdText, bool bNeedTrans, DbParameter[] cmdParms)
        {
            if (args.DbConnection == null)
            {
                if (bNeedTrans)
                {
                    #region -- 没有传入 DbConnection,需要事务 --

                    //using( DbConnection conn = Provider.CreateConnection( args.DBName ) )
                    using (DbConnection conn = Provider.CreateConnection())
                    {
                        try
                        {
                            conn.Open();

                            IsolationLevel level;
                            //if( TS.DW.Setting.SettingDW.TransMode == 0 )
                            if (iTransMode == 0)
                            {
                                level = IsolationLevel.ReadCommitted;
                            }
                            else
                            {
                                level = IsolationLevel.ReadUncommitted;
                            }
                            using (DbTransaction trans = conn.BeginTransaction(level))
                            {
                                using (DbCommand cmd = Provider.CreateCommand())
                                {
                                    int iResult = 0;
                                    try
                                    {
                                        iResult = _ExecuteNonQuery(cmd, conn, trans, cmdType, cmdText, cmdParms);
                                        trans.Commit();
                                    }
                                    catch (Exception ex)
                                    {
                                        try
                                        {
                                            trans.Rollback();
                                        }
                                        catch
                                        {
                                        }
                                        throw ex;
                                    }
                                    return(iResult);
                                }
                            }
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }

                    #endregion -- 没有传入 DbConnection,需要事务 --
                }
                else
                {
                    #region -- 没有传入 DbConnection,不需要事务 --

                    //using( DbConnection conn = Provider.CreateConnection( args.DBName ) )
                    using (DbConnection conn = Provider.CreateConnection())
                    {
                        try
                        {
                            using (DbCommand cmd = Provider.CreateCommand())
                            {
                                return(_ExecuteNonQuery(cmd, conn, null, cmdType, cmdText, cmdParms));
                            }
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }

                    #endregion -- 没有传入 DbConnection,不需要事务 --
                }
            }
            else if (args.DbTrans != null)
            {
                #region -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 --

                using (DbCommand cmd = Provider.CreateCommand())
                {
                    return(_ExecuteNonQuery(cmd, args.DbConnection, args.DbTrans, cmdType, cmdText, cmdParms));
                }

                #endregion -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 --
            }
            else if (bNeedTrans)
            {
                //#region -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 --

                ////throw new Exception( "当需要事务执行时,如果传入 DbConnection,必须同时传入 DbTransaction。" );

                //#endregion -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 --

                #region -- 传入 DbConnection,没有传入 DbTrans,但需要事务:新建事务 --
                IsolationLevel level;
                //if( TS.DW.Setting.SettingDW.TransMode == 0 )
                if (iTransMode == 0)
                {
                    level = IsolationLevel.ReadCommitted;
                }
                else
                {
                    level = IsolationLevel.ReadUncommitted;
                }
                using (DbTransaction trans = args.DbConnection.BeginTransaction(level))
                {
                    using (DbCommand cmd = Provider.CreateCommand())
                    {
                        int iResult = 0;
                        try
                        {
                            iResult = _ExecuteNonQuery(cmd, args.DbConnection, trans, cmdType, cmdText, cmdParms);
                            trans.Commit();
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch
                            {
                            }
                            throw ex;
                        }
                        return(iResult);
                    }
                }
                #endregion -- 传入 DbConnection,没有传入 DbTrans,但需要事务:新建事务 --
            }
            else
            {
                #region -- 传入 DbConnection,没有传入 DbTrans,不需要事务 --

                using (DbCommand cmd = Provider.CreateCommand())
                {
                    return(_ExecuteNonQuery(cmd, args.DbConnection, null, cmdType, cmdText, cmdParms));
                }

                #endregion -- 传入 DbConnection,没有传入 DbTrans,不需要事务 --
            }
        }