public static List <Product> GetProducts() { List <Product> products = new List <Product>(); SqlConnection connection = TechSupportDB.GetConnection(); string selectStatement = "SELECT ProductCode, Name " + "FROM Products " + "ORDER BY Name"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); try { connection.Open(); SqlDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { Product p = new Product(); p.ProductCode = reader["ProductCode"].ToString(); p.Name = reader["Name"].ToString(); products.Add(p); } reader.Close(); } catch (SqlException ex) { throw ex; } finally { connection.Close(); } return(products); }
public static List <Customer> GetCustomers() { List <Customer> customers = new List <Customer>(); SqlConnection connection = TechSupportDB.GetConnection(); string select = "SELECT CustomerID, Name " + "FROM Customers " + "ORDER BY Name"; SqlCommand SelectCommand = new SqlCommand(select, connection); try { connection.Open(); SqlDataReader reader = SelectCommand.ExecuteReader(); while (reader.Read()) { Customer customer = new Customer(); customer.CustomerID = (int)reader["CustomerID"]; customer.Name = reader["Name"].ToString(); customers.Add(customer); } reader.Close(); } catch (SqlException ex) { throw ex; } finally { connection.Close(); } return(customers); }
public static bool AddRegistration(Registration registration) { SqlConnection connection = TechSupportDB.GetConnection(); string insertStatement = "INSERT Registrations " + "(CustomerID, ProductCode, RegistrationDate) " + "VALUES (@CustomerID, @ProductCode, @RegistrationDate)"; SqlCommand insertCommand = new SqlCommand(insertStatement, connection); insertCommand.Parameters.AddWithValue( "@CustomerID", registration.CustomerID); insertCommand.Parameters.AddWithValue( "@ProductCode", registration.ProductCode); insertCommand.Parameters.AddWithValue( "@RegistrationDate", registration.RegistrationDate); try { connection.Open(); int count = insertCommand.ExecuteNonQuery(); if (count > 0) { return(true); } else { return(false); } } catch (SqlException) { return(false); } finally { connection.Close(); } }