Пример #1
0
        public IResultSet Exec(string procName, object[] parameters, int[] outputs)
        {
            ErrorIfClosed();
            SqlResultSet  rs     = null;
            IDbCommand    cmd    = null;
            IDataReader   reader = null;
            StringBuilder buf    = new StringBuilder("exec ").Append(procName);

            try
            {
                InternalOpen();
                int[] sortedOutputs = null;
                if (outputs != null)
                {
                    sortedOutputs = SortedCopy(outputs);
                }

                cmd             = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.Transaction = transaction;
                IDbDataParameter[] outputParams = SetupCommand(cmd, parameters, sortedOutputs, buf);

                buf.Append(";");
                cmd.CommandText = buf.ToString();
                cmd.Prepare();

                TraceObject.Instance.TraceCommand(cmd);

                reader = cmd.ExecuteReader();

                // setup the result set
                int       sz    = 0;
                string[]  names = null;
                DataTable meta  = reader.GetSchemaTable();
                if (meta != null)
                {
                    names = new string[meta.Rows.Count];
                    foreach (DataRow datarow in meta.Rows)
                    {
                        // NOTE: this assumes that the name of the column
                        // is in the first field of the schema table.
                        // This is the case for .NET 2.0.
                        names[sz++] = (string)datarow[0];
                    }
                    meta.Dispose();
                    meta = null;
                }
                rs = new SqlResultSet(names);

                while (reader.Read())
                {
                    object[] row = new object[sz];
                    reader.GetValues(row);
                    rs.AddRow(row);
                }
                // this is necessary to get output values
                reader.Close();

                if (sortedOutputs != null)
                {
                    for (int i = 0; i < sortedOutputs.Length; i++)
                    {
                        int    index = sortedOutputs[i];
                        object val   = outputParams[i].Value;
                        if (val == DBNull.Value)
                        {
                            val = null;
                        }
                        parameters[index] = val;
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    reader.Dispose();
                    reader = null;
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                InternalClose();
            }
            return(rs);
        }
Пример #2
0
        public IResultSet Exec(string procName, string[] parameterName, object[] parameters, string[] outputParameterName, int?[] outputParameterSize, DbType[] outputParameterType,
                               ParameterDirection[] outputParameterDirection, out object[] outParameterResult)
        {
            ErrorIfClosed();
            SqlResultSet rs     = null;
            IDbCommand   cmd    = null;
            IDataReader  reader = null;

            try
            {
                InternalOpen();
                int index = 0;
                outParameterResult = new object[outputParameterName.Length];

                cmd             = connection.CreateCommand();
                cmd.CommandText = procName;
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Transaction = transaction;
                this.AddParameters(cmd, parameterName, parameters, transaction);
                IDbDataParameter[] outputParams = this.AddOutputReturnParameters(cmd, outputParameterName,
                                                                                 outputParameterSize, outputParameterType, outputParameterDirection, transaction);

                TraceObject.Instance.TraceCommand(cmd);

                reader = cmd.ExecuteReader();

                // setup the result set
                int       sz    = 0;
                string[]  names = null;
                DataTable meta  = reader.GetSchemaTable();
                if (meta != null)
                {
                    names = new string[meta.Rows.Count];
                    foreach (DataRow datarow in meta.Rows)
                    {
                        // NOTE: this assumes that the name of the column
                        // is in the first field of the schema table.
                        // This is the case for .NET 2.0.
                        names[sz++] = (string)datarow[0];
                    }
                    meta.Dispose();
                    meta = null;
                }
                rs = new SqlResultSet(names);

                while (reader.Read())
                {
                    object[] row = new object[sz];
                    reader.GetValues(row);
                    rs.AddRow(row);
                }
                // this is necessary to get output values
                reader.Close();

                for (int i = 0; i < outputParams.Length; i++)
                {
                    outParameterResult[index] = outputParams[i].Value;
                    index++;
                }
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    reader.Dispose();
                    reader = null;
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }

                InternalClose();
            }
            return(rs);
        }
Пример #3
0
        public virtual void Select(IDb db, string procName, string[] parameterName, object[] parameters, string[] outputParameterName, int?[] outputParameterSize, DbType[] outputParameterType,
                                   ParameterDirection[] outputParameterDirection, out object[] outParameterResult, IList list)
        {
            ErrorIfSPResult();

            IDbConnection  conn        = db.Connection;
            IDbTransaction transaction = db.Transaction;

            if (transaction != null)
            {
                conn = transaction.Connection;
            }

            SqlResultSet rs     = null;
            IDbCommand   cmd    = null;
            IDataReader  reader = null;

            try
            {
                int index = 0;
                outParameterResult = new object[outputParameterName.Length];

                cmd             = conn.CreateCommand();
                cmd.CommandText = procName;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Transaction = transaction;
                db.AddParameters(cmd, parameterName, parameters, transaction);
                IDbDataParameter[] outputParams = db.AddOutputReturnParameters(cmd, outputParameterName,
                                                                               outputParameterSize, outputParameterType, outputParameterDirection, transaction);

                TraceObject.Instance.TraceCommand(cmd);

                reader = cmd.ExecuteReader();
                FillByName(reader, list);

                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    reader.Dispose();
                    reader = null;
                }

                for (int i = 0; i < outputParams.Length; i++)
                {
                    outParameterResult[index] = outputParams[i].Value;
                    index++;
                }
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    reader.Dispose();
                    reader = null;
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
            }
            FireTriggers(list, new TriggerEventArgs(db, Timing.AfterActivate));
        }