From 06e167a75fe80395f3cb243a21bc5c51ac8efe9f Mon Sep 17 00:00:00 2001 From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com> Date: Tue, 19 May 2020 21:42:44 -0700 Subject: [PATCH] Implement GetSchoolByName for school editor Bugfix .single() to .SingleOrDefault() Move school stuff to a new region. --- SqliteDataAccess.cs | 57 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/SqliteDataAccess.cs b/SqliteDataAccess.cs index c1e0749..50e7bdb 100644 --- a/SqliteDataAccess.cs +++ b/SqliteDataAccess.cs @@ -64,7 +64,7 @@ namespace DBWizard using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { string strQuery = "SELECT * FROM students WHERE id=@id"; - Student stu_out = cnn.Query(strQuery, new { id = dbid_in }).Single(); + Student stu_out = cnn.Query(strQuery, new { id = dbid_in }).SingleOrDefault(); return stu_out; } @@ -155,7 +155,7 @@ namespace DBWizard string strQuery = "INSERT INTO PARENTS (FirstName,LastName,PhoneNumber,EmailAddress)" + "VALUES (@FirstName,@LastName,@PhoneNumber,@EmailAddress);" + "SELECT MAX(parent_id) as parent_id from parents;"; - int parent_id_out = cnn.Query(strQuery, par).Single(); + int parent_id_out = cnn.Query(strQuery, par).SingleOrDefault(); return parent_id_out; } } @@ -222,22 +222,23 @@ namespace DBWizard #endregion - #region Misc - /* Returns the list of available programs from the DB to fill the Programs drop-down. - * INPUT: no args - * OUTPUT: List + #region school + // ***************** Create ***************** + /* Inserts a new school to the SQL database. + * INPUT: School + * OUTPUT: new row to SQL database, null to program. */ - internal List GetPrograms() + internal void InsertNewSchool(School sch) { using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { - string strQuery = "SELECT name FROM programs;"; - - var output = cnn.Query(strQuery, new DynamicParameters()); - return output.ToList(); + string strQuery = "INSERT INTO SCHOOLS (name) VALUES (@name)"; + cnn.Execute(strQuery, sch); } } + // ***************** Read ***************** + /* Returns the list of available schools from the DB to fill the Schools drop-down. * INPUT: no args * OUTPUT: List @@ -252,6 +253,40 @@ namespace DBWizard return output.ToList(); } } + + /* Returns the a single School from the DB by name. + * Needed for the school editor. + * INPUT: string school_name_in + * OUTPUT: School + */ + internal School GetSchoolByName(string school_name_in) + { + using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) + { + string strQuery = "SELECT name FROM Schools WHERE name = @school_name;"; + + School output = cnn.Query(strQuery, new { school_name = school_name_in }).SingleOrDefault(); + return output; + } + } + #endregion + + #region Misc + /* Returns the list of available programs from the DB to fill the Programs drop-down. + * INPUT: no args + * OUTPUT: List + */ + internal List GetPrograms() + { + using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) + { + string strQuery = "SELECT name FROM programs;"; + + var output = cnn.Query(strQuery, new DynamicParameters()); + return output.ToList(); + } + } + #endregion #region reports