public void AddNewDebtor(Debtor debtor, Debt debt) { SqlConnection sqlcon = new SqlConnection(connectionString); string debtorInsert = "INSERT INTO Debtors" + "(DebtorId, Name, TotalDebt)" + "VALUES" + $"({debtor.Id}, '{debtor.Name}', {debtor.TotalDebt})"; string debtInsert = "INSERT INTO Debts" + "(DebtorId, Date, DebtValue)" + "VALUES" + $"({debtor.Id}, '{debt.Date}', {debtor.TotalDebt})"; try { sqlcon.Open(); SqlCommand debtorInsertCommand = new SqlCommand(debtorInsert, sqlcon); SqlCommand debtInsertCommand = new SqlCommand(debtInsert, sqlcon); debtorInsertCommand.ExecuteNonQuery(); debtInsertCommand.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine(e.Message); } finally { sqlcon.Close(); } }
public ICollection <Debt> GetDebtsFor(int ID) { SqlConnection sqlcon = new SqlConnection(connectionString); ObservableCollection <Debt> debts = new ObservableCollection <Debt>(); SqlCommand cmd = new SqlCommand("SELECT * FROM Debts WHERE DebtorID=" + ID, sqlcon); try { sqlcon.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Debt debt = new Debt((int)reader["DebtValue"], (DateTime)reader["Date"]); debts.Add(debt); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { sqlcon.Close(); } return(debts); }