public Business Add(Business business) { SqlConnection conn = null; int newIdent = -1; try { conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataServices"].ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("AddCompany", conn); cmd.CommandType = CommandType.StoredProcedure; SqlParameter outval = new SqlParameter(); cmd.Parameters.AddWithValue("@name", business.name); outval.ParameterName = "@id"; outval.Direction = ParameterDirection.Output; outval.Value = newIdent; cmd.Parameters.Add(outval); cmd.ExecuteNonQuery(); newIdent = (int)outval.Value; cmd.ExecuteNonQuery(); return Get(newIdent); } catch (Exception e) { this.WriteToLog("Add", e); return null; } finally { if (conn != null) { conn.Close(); } } }
public Business Get(int companyId) { SqlConnection conn = null; SqlDataReader dr = null; Business result = new Business(); try { conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataServices"].ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("GetCompany", conn); cmd.Parameters.AddWithValue("@companyId", companyId); cmd.CommandType = CommandType.StoredProcedure; dr = cmd.ExecuteReader(); while (dr.Read()) { result.id = dr.GetInt32(0); result.name = dr.GetValue(1).ToString(); } return result; } catch (Exception e) { WriteToLog("Get(companyid)", e); return null; } finally { if (conn != null) { conn.Close(); } } }
public IEnumerable<Business> Get() { SqlConnection conn = null; SqlDataReader dr = null; List<Business> results = new List<Business>(); try { conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataServices"].ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("GetCompanies", conn); cmd.CommandType = CommandType.StoredProcedure; dr = cmd.ExecuteReader(); while (dr.Read()) { Business l = new Business(); l.id = dr.GetInt32(0); l.name = dr.GetValue(1).ToString(); results.Add(l); } return results; } catch (Exception e) { WriteToLog("Get", e); return results; } finally { if (conn != null) { conn.Close(); } } }