Ejemplo n.º 1
0
        public static ReadOnlyCollection <Unidad> GetAll(int companyId)
        {
            /* CREATE PROCEDURE Provider_GetByCompany
             *   @CompanyId int */
            List <Unidad> res = new List <Unidad>();

            using (SqlCommand cmd = new SqlCommand("Unidad_GetAll"))
            {
                cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["cns"].ConnectionString);
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(DataParameter.Input("@CompanyId", companyId));
                    cmd.Connection.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        Unidad objetivo = new Unidad()
                        {
                            Id          = rdr.GetInt32(ColumnsUnidadGet.Id),
                            Description = rdr.GetString(ColumnsUnidadGet.Description),
                            CompanyId   = companyId,
                            CreatedBy   = new ApplicationUser()
                            {
                                Id       = rdr.GetInt32(ColumnsUnidadGet.CreatedBy),
                                UserName = rdr.GetString(ColumnsUnidadGet.CreatedByName)
                            },
                            CreatedOn  = rdr.GetDateTime(ColumnsUnidadGet.CreatedOn),
                            ModifiedBy = new ApplicationUser()
                            {
                                Id       = rdr.GetInt32(ColumnsUnidadGet.ModifiedBy),
                                UserName = rdr.GetString(ColumnsUnidadGet.ModifiedByName)
                            },
                            ModifiedOn   = rdr.GetDateTime(ColumnsUnidadGet.ModifiedOn),
                            Active       = rdr.GetBoolean(ColumnsUnidadGet.Active),
                            CanBeDeleted = rdr.GetInt32(ColumnsUnidadGet.Deletable) == 1
                        };

                        res.Add(objetivo);
                    }
                }
                finally
                {
                    if (cmd.Connection.State != ConnectionState.Closed)
                    {
                        cmd.Connection.Close();
                    }
                }
            }

            return(new ReadOnlyCollection <Unidad>(res));
        }
Ejemplo n.º 2
0
        public static Unidad GetById(int objetivoId, int companyId)
        {
            /* CREATE PROCEDURE Unidad_GetByCompany
             *   @UnidadId int */
            Unidad res = Unidad.Empty;

            using (SqlCommand cmd = new SqlCommand("Unidad_GetById"))
            {
                cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["cns"].ConnectionString);
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(DataParameter.Input("@UnidadId", objetivoId));
                    cmd.Parameters.Add(DataParameter.Input("@CompanyId", companyId));
                    cmd.Connection.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        res.Id          = rdr.GetInt32(ColumnsUnidadGet.Id);
                        res.Description = rdr.GetString(ColumnsUnidadGet.Description);
                        res.CompanyId   = companyId;
                        res.CreatedBy   = new ApplicationUser()
                        {
                            Id       = rdr.GetInt32(ColumnsUnidadGet.CreatedBy),
                            UserName = rdr.GetString(ColumnsUnidadGet.CreatedByName)
                        };
                        res.CreatedOn  = rdr.GetDateTime(ColumnsUnidadGet.CreatedOn);
                        res.ModifiedBy = new ApplicationUser()
                        {
                            Id       = rdr.GetInt32(ColumnsUnidadGet.ModifiedBy),
                            UserName = rdr.GetString(ColumnsUnidadGet.ModifiedByName)
                        };
                        res.ModifiedOn   = rdr.GetDateTime(ColumnsUnidadGet.ModifiedOn);
                        res.Active       = rdr.GetBoolean(ColumnsUnidadGet.Active);
                        res.CanBeDeleted = rdr.GetInt32(ColumnsUnidadGet.Deletable) == 1;
                    }
                }
                finally
                {
                    if (cmd.Connection.State != ConnectionState.Closed)
                    {
                        cmd.Connection.Close();
                    }
                }
            }

            return(res);
        }