示例#1
0
        public DataSet GetDataSet_Cmd(string commandText, CommandType commandType, params DbParameter[] parameters)
        {
            SqlTransaction trans       = Transaction;
            SqlConnection  conn        = null;
            SqlCommand     cmd         = null;
            bool           needDispose = true; //针对没有事务的情况

            try
            {
                cmd = new SqlCommand();
                cmd.CommandTimeout = CommandTimeout;
                if (trans == null)
                {
                    conn = this.Connection;

                    if (conn == null)
                    {
                        conn = OpenNew(this.connectionString, false);
                    }
                    else
                    {
                        needDispose = false;//采用共享的数据库连接,使用完毕后不需关闭
                    }
                    DbHelperUtil.PrepareCommand(cmd, conn, (SqlTransaction)null, commandType, commandText, parameters);
                }
                else
                {
                    DbHelperUtil.PrepareCommand(cmd, trans.Connection, trans, commandType, commandText, parameters);
                }

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    return(ds);
                }
            }
            catch (Exception ex)
            {
                RollBack();
                if (ex is SqlException && ((SqlException)ex).Number == 99999)
                {
                    throw new RFException(ex.Message);
                }
                throw ex;
            }
            finally
            {
                if (needDispose)
                {
                    Close(conn);
                }
                if (cmd != null)
                {
                    cmd.Parameters.Clear();
                }
            }
        }
示例#2
0
        public object GetValue(string commandText, CommandType commandType, params DbParameter[] parameters)
        {
            SqlTransaction trans       = Transaction;
            SqlConnection  conn        = null;
            SqlCommand     cmd         = null;
            bool           needDispose = true; //针对没有事务的情况

            try
            {
                cmd = new SqlCommand();
                cmd.CommandTimeout = CommandTimeout;
                if (trans == null)
                {
                    conn = this.Connection;

                    if (conn == null)
                    {
                        conn = OpenNew(this.connectionString, false);
                    }
                    else
                    {
                        needDispose = false;//采用共享的数据库连接,使用完毕后不需关闭
                    }
                    DbHelperUtil.PrepareCommand(cmd, conn, (SqlTransaction)null, commandType, commandText, parameters);
                }
                else
                {
                    DbHelperUtil.PrepareCommand(cmd, trans.Connection, trans, commandType, commandText, parameters);
                }

                object obj = cmd.ExecuteScalar();//结果集中的第一行第一列
                return(obj);
            }
            catch (Exception ex)
            {
                RollBack();
                if (ex is SqlException && ((SqlException)ex).Number >= 50000)
                {
                    throw new RFException(ex.Message);
                }
                throw ex;
            }
            finally
            {
                if (needDispose)
                {
                    Close(conn);
                }
                if (cmd != null)
                {
                    cmd.Parameters.Clear();
                }
            }
        }