示例#1
0
        public Result <List <T> > ExecuteWithResults <T>(string query, ConexionParameters parameters, out List <T> results)
        {
            var r = ExecuteWithResults <T>(query, parameters);

            results = r.Data as List <T>;

            return(r);
        }
示例#2
0
        public bool RecordsetsExecute(string storeProcedure, ConexionParameters parameters)
        {
            bool result = false;

            try
            {
                if (_transaction == null && this._conexion.State != ConnectionState.Open)
                {
                    this._conexion.Open();
                }

                this._commandRecordsets = this._conexion.CreateCommand();

                _commandRecordsets.CommandText    = storeProcedure;
                _commandRecordsets.Transaction    = _transaction;
                _commandRecordsets.CommandTimeout = ConexionTimeOut;

                if (parameters != null)
                {
                    _parametersRecordsets = parameters;

                    _commandRecordsets.CommandType = CommandType.StoredProcedure;

                    foreach (var p in parameters.Parameters)
                    {
                        var pt = _commandRecordsets.CreateParameter();
                        pt.ParameterName = p.Name;
                        pt.DbType        = p.Type;
                        pt.Value         = p.Value;
                        pt.Size          = p.Size;
                        pt.Direction     = p.Direction;

                        _commandRecordsets.Parameters.Add(pt);
                    }
                }

                _readerRecordsets = _commandRecordsets.ExecuteReader();

                switch (this._conexionType)
                {
                case ConexionType.MSSQLServer:
                    result = ((SqlDataReader)_readerRecordsets).HasRows;
                    break;
                    //case ConexionType.PostgreSQL:
                    //    result = ((PgSqlDataReader)_readerRecordsets).HasRows;
                    //    break;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ThrowExceptionExecuteMessage + ex.Message, ex);
            }

            return(result);
        }
示例#3
0
        private Result <T> ResultBuilder <T>(ConexionParameters parameters)
        {
            var r = ResultBuilder(parameters);

            var result = new Result <T>()
            {
                Value   = r.Value,
                Message = r.Message
            };

            return(result);
        }
示例#4
0
        private Result ResultBuilder(ConexionParameters parameters)
        {
            var result = new Result(true, "the query has been executed correctly");

            var resultParameter = parameters?.Parameters?.FirstOrDefault(p => p.Name.ToLower() == "@result" || p.Name.ToLower() == "@presult" || p.Name.ToLower() == "@resultado" || p.Name.ToLower() == "@presultado");

            if (resultParameter != null)
            {
                result.Value = new Castable(resultParameter.Value).ToBoolean();
            }

            var messageParameter = parameters?.Parameters?.FirstOrDefault(p => p.Name.ToLower() == "@msg" || p.Name.ToLower() == "@pmsg" || p.Name.ToLower() == "@message" || p.Name.ToLower() == "@pmessage" || p.Name.ToLower() == "@mensaje" || p.Name.ToLower() == "@pmensaje");

            if (messageParameter != null)
            {
                result.Message = new Castable(messageParameter.Value).ToString();
            }

            return(result);
        }
示例#5
0
        public void ExecuteWithResults(string query, ConexionParameters parameters, Action <ConexionCastableRow> action)
        {
            try
            {
                if (_transaction == null && this._conexion.State != ConnectionState.Open)
                {
                    this._conexion.Open();
                }

                using (var cmd = this._conexion.CreateCommand())
                {
                    cmd.CommandText    = query;
                    cmd.Transaction    = _transaction;
                    cmd.CommandTimeout = ConexionTimeOut;

                    if (parameters != null)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        foreach (var p in parameters.Parameters)
                        {
                            var pt = cmd.CreateParameter();
                            pt.ParameterName = p.Name;
                            pt.DbType        = p.Type;
                            pt.Value         = p.Value;
                            pt.Size          = p.Size;
                            pt.Direction     = p.Direction;

                            cmd.Parameters.Add(pt);
                        }
                    }

                    var lector = cmd.ExecuteReader();


                    while (lector.Read())
                    {
                        var castableRow = new ConexionCastableRow(lector);

                        action(castableRow);
                    }

                    lector.Close();

                    if (parameters != null)
                    {
                        foreach (IDbDataParameter p in cmd.Parameters)
                        {
                            var param = parameters.Parameters.FirstOrDefault(x => x.Name == p.ParameterName);
                            if (param != null)
                            {
                                param.Value = p.Value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ThrowExceptionExecuteMessage + ex.Message, ex);
            }
            finally
            {
                if (_transaction == null && _closeConnection)
                {
                    this._conexion.Close();
                }
            }
        }
示例#6
0
        public Result <List <T> > ExecuteWithResults <T>(string query, ConexionParameters parameters)
        {
            List <T> lst = new List <T>();

            try
            {
                if (_transaction == null && this._conexion.State != ConnectionState.Open)
                {
                    this._conexion.Open();
                }

                using (var cmd = this._conexion.CreateCommand())
                {
                    cmd.CommandText    = query;
                    cmd.Transaction    = _transaction;
                    cmd.CommandTimeout = ConexionTimeOut;

                    if (parameters != null)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        foreach (var p in parameters.Parameters)
                        {
                            var pt = cmd.CreateParameter();
                            pt.ParameterName = p.Name;
                            pt.DbType        = p.Type;
                            pt.Value         = p.Value;
                            pt.Size          = p.Size;
                            pt.Direction     = p.Direction;

                            cmd.Parameters.Add(pt);
                        }
                    }

                    var lector = cmd.ExecuteReader();

                    lst = ResultsInDataReader <T>(ref lector);

                    lector.Close();

                    if (parameters != null)
                    {
                        foreach (IDbDataParameter p in cmd.Parameters)
                        {
                            var param = parameters.Parameters.FirstOrDefault(x => x.Name == p.ParameterName);
                            if (param != null)
                            {
                                param.Value = p.Value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ThrowExceptionExecuteMessage + ex.Message, ex);
            }
            finally
            {
                if (_transaction == null && _closeConnection)
                {
                    this._conexion.Close();
                }
            }

            var result = ResultBuilder <List <T> >(parameters);

            result.Data = lst;

            return(result);
        }
示例#7
0
        public Castable ExecuteScalar(string storeProcedure, ConexionParameters parameters)
        {
            Castable result;

            try
            {
                if (_transaction == null && this._conexion.State != ConnectionState.Open)
                {
                    this._conexion.Open();
                }

                using (var cmd = this._conexion.CreateCommand())
                {
                    cmd.CommandText    = storeProcedure;
                    cmd.Transaction    = _transaction;
                    cmd.CommandTimeout = ConexionTimeOut;

                    if (parameters != null)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        foreach (var p in parameters.Parameters)
                        {
                            var pt = cmd.CreateParameter();
                            pt.ParameterName = p.Name;
                            pt.DbType        = p.Type;
                            pt.Value         = p.Value;
                            pt.Size          = p.Size;
                            pt.Direction     = p.Direction;

                            cmd.Parameters.Add(pt);
                        }
                    }

                    var scalar = cmd.ExecuteScalar();
                    result = new Castable(scalar);

                    if (parameters != null)
                    {
                        foreach (IDbDataParameter p in cmd.Parameters)
                        {
                            var param = parameters.Parameters.FirstOrDefault(x => x.Name == p.ParameterName);
                            if (param != null)
                            {
                                param.Value = p.Value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ThrowExceptionExecuteMessage + ex.Message, ex);
            }
            finally
            {
                if (_transaction == null && _closeConnection)
                {
                    this._conexion.Close();
                }
            }

            return(result);
        }
示例#8
0
        public Result <T> ExecuteScalar <T>(string storeProcedure, ConexionParameters parameters)
        {
            object scalar;

            try
            {
                if (_transaction == null && this._conexion.State != ConnectionState.Open)
                {
                    this._conexion.Open();
                }

                using (var cmd = this._conexion.CreateCommand())
                {
                    cmd.CommandText    = storeProcedure;
                    cmd.Transaction    = _transaction;
                    cmd.CommandTimeout = ConexionTimeOut;

                    if (parameters != null)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        foreach (var p in parameters.Parameters)
                        {
                            var pt = cmd.CreateParameter();
                            pt.ParameterName = p.Name;
                            pt.DbType        = p.Type;
                            pt.Value         = p.Value;
                            pt.Size          = p.Size;
                            pt.Direction     = p.Direction;

                            cmd.Parameters.Add(pt);
                        }
                    }

                    scalar = cmd.ExecuteScalar();

                    if (parameters != null)
                    {
                        foreach (IDbDataParameter p in cmd.Parameters)
                        {
                            var param = parameters.Parameters.FirstOrDefault(x => x.Name == p.ParameterName);
                            if (param != null)
                            {
                                param.Value = p.Value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ThrowExceptionExecuteMessage + ex.Message, ex);
            }
            finally
            {
                if (_transaction == null && _closeConnection)
                {
                    this._conexion.Close();
                }
            }

            var result = ResultBuilder <T>(parameters);

            try
            {
                var type = typeof(Result <T>);

                foreach (PropertyInfo pro in type.GetProperties())
                {
                    string propertyName = pro.Name.ToLowerInvariant();

                    if (propertyName == "data")
                    {
                        pro.SetValue(result, scalar, null);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return(result);
        }
示例#9
0
        public Result ExecuteWithResults(string query, ConexionParameters parameters, out DataSet ds)
        {
            try
            {
                if (_transaction == null && this._conexion.State != ConnectionState.Open)
                {
                    this._conexion.Open();
                }

                using (var cmd = this._conexion.CreateCommand())
                {
                    cmd.CommandText    = query;
                    cmd.Transaction    = _transaction;
                    cmd.CommandTimeout = ConexionTimeOut;

                    if (parameters != null)
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        foreach (var p in parameters.Parameters)
                        {
                            var pt = cmd.CreateParameter();
                            pt.ParameterName = p.Name;
                            pt.DbType        = p.Type;
                            pt.Value         = p.Value;
                            pt.Size          = p.Size;
                            pt.Direction     = p.Direction;

                            cmd.Parameters.Add(pt);
                        }
                    }

                    ds = new DataSet();

                    switch (this._conexionType)
                    {
                    case ConexionType.MSSQLServer:
                        var sqlAdapter = new SqlDataAdapter((SqlCommand)cmd);
                        sqlAdapter.Fill(ds);
                        break;
                        //case ConexionType.PostgreSQL:
                        //    var pgAdapter = new PgSqlDataAdapter((PgSqlCommand)cmd);
                        //    pgAdapter.Fill(dt);
                        //    break;
                    }

                    if (parameters != null)
                    {
                        foreach (IDbDataParameter p in cmd.Parameters)
                        {
                            var param = parameters.Parameters.FirstOrDefault(x => x.Name == p.ParameterName);
                            if (param != null)
                            {
                                param.Value = p.Value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ThrowExceptionExecuteMessage + ex.Message, ex);
            }
            finally
            {
                if (_transaction == null && _closeConnection)
                {
                    this._conexion.Close();
                }
            }

            return(ResultBuilder(parameters));
        }