// Add account to database, using input to fill the record fields // If write to DB is not successful, throw exception to caller public static void addAccount(int CustomerId, string accountNumber, decimal balance) { try { using (var db = new LargeBankEntities()) { // Set up the account record Account acct = new Account(); acct.CustomerId = CustomerId; acct.CreatedDate = DateTime.Now; acct.AccountNumber = accountNumber; acct.Balance = balance; // Add the customer and save the changes to the DB db.Accounts.Add(acct); db.SaveChanges(); } } // Let calling method handle exception if it occurs catch (Exception) { throw; } }
// Add customer to database, using input to fill the record fields // Set created date to current date // After writing, get the customer ID, and return it // If read/write to DB is not successful, throw exception to caller public static int addCustomer(string firstName, string lastName, string address1, string address2, string city, string state, string zip) { try { using (var db = new LargeBankEntities()) { // Set up the customer record Customer c = new Customer(); DateTime curDate = DateTime.Now; c.CreatedDate = curDate; c.FirstName = firstName; c.LastName = lastName; c.Address1 = address1; c.Address2 = address2; c.City = city; c.State = state; c.Zip = zip; //Console.WriteLine("Added first name: {0}, last name: {0}, created: {2}", // c.FirstName, c.LastName, curDate); // Add the customer and save the changes to the DB db.Customers.Add(c); db.SaveChanges(); // Get the customer ID return getCustomerID(c.FirstName, c.LastName, curDate); } } // Let calling method handle exception if it occurs catch (Exception) { throw; } }