public void Add(Customer customer) { db.Open(); try { SqlCommand command = new SqlCommand( "insert into customers (Id, Name, RegisteredAt, Address, city, State, PostalCode, Phone, AccountCredit) values (@id, @name, @registered_at, @address, @city, @state, @postal_code, @phone, @account_credit)", this.db); command.Parameters.AddWithValue("@id", customer.Id); command.Parameters.AddWithValue("@name", customer.Name); command.Parameters.AddWithValue("@registered_at", customer.RegisteredAt); command.Parameters.AddWithValue("@address", customer.Address); command.Parameters.AddWithValue("@city", customer.City); command.Parameters.AddWithValue("@state", customer.State); command.Parameters.AddWithValue("@postal_code", customer.PostalCode); command.Parameters.AddWithValue("@phone", customer.Phone); command.Parameters.AddWithValue("@account_credit", customer.AccountCredit); command.ExecuteNonQuery(); } finally { db.Close(); } }
public static void SeedCustomers() { // Read the contents of the file string s = File.ReadAllText("..\\..\\..\\MovieRentals\\App_Data\\customers.json"); // Parse the contents using JSON.NET JArray data = (JArray)JsonConvert.DeserializeObject(s); CustomerRepository repository = new CustomerRepository(); // Process the data foreach (JToken token in data) { Customer c = new Customer(); c.Id = token["id"].Value<int>(); c.Name = token["name"].Value<string>(); c.RegisteredAt = token["registered_at"].Value<string>(); c.Address = token["address"].Value<string>(); c.City = token["city"].Value<string>(); c.State = token["state"].Value<string>(); c.PostalCode = token["postal_code"].Value<string>(); c.Phone = token["phone"].Value<string>(); c.AccountCredit = token["account_credit"].Value<decimal>(); repository.Add(c); } }