public static CustomerDatabaseError AddToDatabase(Customer cust) { SQLiteConnection connection = new SQLiteConnection(DatabaseManager.CONNECTION_STRING); SQLiteCommand command = new SQLiteCommand("INSERT INTO customers(name, phone, adress, join_date) VALUES(@name,@phone,@adress,CURRENT_TIMESTAMP)", connection); command.Parameters.AddWithValue("@name", cust.Name); command.Parameters.AddWithValue("@phone", cust.Name); command.Parameters.AddWithValue("@adress", cust.Name); try { connection.Open(); } catch (SQLiteException ex) { return CustomerDatabaseError.ERR_DATABASE_CONNECTION; } try { command.ExecuteNonQuery(); } catch (SQLiteException ex) { return CustomerDatabaseError.ERR_UKNOWN; } return CustomerDatabaseError.ERR_SUCCESS; }
public static bool CreateCard(Customer customer) { SQLiteConnection connection = new SQLiteConnection(DatabaseManager.CONNECTION_STRING); SQLiteCommand command = new SQLiteCommand(@"INSERT INTO clubcard(customer_id, activation_date, expire_date, balance) VALUES(@customerID, CURRENT_TIMESTAMP, @expireDate, 0);", connection); command.Parameters.AddWithValue("@customerID", customer.ID); command.Parameters.AddWithValue("@expireDate", DateTime.Now.AddYears(Helper.CLUBCARD_EXPIRE_PERIOD)); try { connection.Open(); } catch (SQLiteException ex) { return false; } try { command.ExecuteNonQuery(); } catch (SQLiteException ex) { return false; } return true; }
private void btnAddCustomer_Click(object sender, EventArgs e) { string phone = txtCustomerAddPhone.Text; string address = txtCustomerAddAddress.Text; string name = txtCustomerAddName.Text; Customer nCust = new Customer(name, address, phone); var result = Customer.AddToDatabase(nCust); if (result == Customer.CustomerDatabaseError.ERR_SUCCESS) { if (MessageBox.Show("Customer added successfully. Do you want to add a clubcard for this customer?", "Notice", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (ClubCard.CreateCard(nCust)) { MessageBox.Show("Successfully created a clubcard!", "Notice"); } } } }
public static ClubCard CreateCard(Customer customer, float amount=0) { SQLiteConnection connection = new SQLiteConnection(DatabaseManager.CONNECTION_STRING); SQLiteCommand command = new SQLiteCommand("INSERT INTO clubcards (customer_id, balance) VALUES (@customerId,@amount)", connection); command.Parameters.AddWithValue("@customerId", customer.ID); command.Parameters.AddWithValue("@amount", amount); try { connection.Open(); } catch { return null; } try { command.ExecuteNonQuery(); } catch { return null; } command.CommandText = @"select last_insert_rowid()"; int cardID = 0; try { cardID = Convert.ToInt32(command.ExecuteScalar()); } catch { } connection.Close(); ClubCard c = new ClubCard(); c.Balance = amount; c.CardID = cardID; c.CustomerID = customer.ID; return c; }