public static Company GetCompany(int id) { Company company = null; string sqlExpression = "SELECT * FROM Company WHERE Id = @id AND Deleted = 0"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); command.Parameters.Add("@id", SqlDbType.Int); command.Parameters["@id"].Value = id; SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { int companyTypeId = (int)reader["CompanyTypeId"]; string name = (string)reader["CompanyName"]; string address = (string)reader["CompanyAddress"]; Director director = new Director() { InitState = InitializationState.INITIALIZATION_NEEDED }; Chancery chancery = new Chancery() { InitState = InitializationState.INITIALIZATION_NEEDED }; CompanyType companyType = SqlCompanyType.GetCompanyType(companyTypeId); company = new Company(director, companyType, chancery, name, address) { Id = id }; } } } return(company); }
public static List <Company> GetAllCompanies() { List <Company> companies = new List <Company>(); string sqlExpression = "SELECT * FROM Company WHERE Deleted = 0"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand expression = new SqlCommand(sqlExpression, connection); SqlDataReader reader = expression.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { int id = (int)reader["Id"]; int companyTypeId = (int)reader["CompanyTypeId"]; string name = (string)reader["CompanyName"]; string address = (string)reader["CompanyAddress"]; Director director = new Director() { InitState = InitializationState.INITIALIZATION_NEEDED }; Chancery chancery = new Chancery() { InitState = InitializationState.INITIALIZATION_NEEDED }; CompanyType companyType = SqlCompanyType.GetCompanyType(companyTypeId); Company company = new Company(director, companyType, chancery, name, address) { Id = id }; companies.Add(company); } } } return(companies); }