From f8bafc8b6dac41c9c1836435fb0580dce9f4c9ae Mon Sep 17 00:00:00 2001 From: Lunastra Date: Sat, 11 Dec 2021 15:06:44 -0800 Subject: [PATCH] Add writing, deleting data to SqlDAO --- .../Implementations/SqlDAO.cs | 82 ++++++++++++++----- Source Code/main/Controller.cs | 40 +++++---- 2 files changed, 86 insertions(+), 36 deletions(-) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index ea7e9e3..87e0367 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs @@ -22,47 +22,65 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess Console.WriteLine("Connection established"); } catch { - //conn = null; + //_conn = null; Console.WriteLine("Error when creating a connection"); throw; } - } // Getter and setter for Odbc public OdbcConnection Connection { get; set; } - // Makre sure to Check for instanceof() before casting to a SQLReader in the controller + // Closing a connection here will cause a problem + // Make sure whoever called this need to close the connection until we can fixed it. public Object? ReadData(string cmd) { try { _conn.Open(); OdbcCommand command = new OdbcCommand(cmd, _conn); - OdbcDataReader reader = command.ExecuteReader(); - // Execute the command to query the data - //using SqlDataReader sqlReader = command.ExecuteReader(); - - // while (myReader) - // Print to console - //conn.Close(); - //conn.Dispose(); - //return sqlReader; + //_conn.Close(); return reader; + } catch (Exception e) { + Console.WriteLine("Error when Reading data from databse."); Console.WriteLine(e.Message); return null; } + //finally + //{ + // _conn.Close(); + //} } // The also Identical to update data, maybe only one method is enough public bool DeleteData(string cmd) { - throw new NotImplementedException(); + try + { + _conn.Open(); + + OdbcCommand command = new OdbcCommand(cmd, _conn); + command.ExecuteNonQuery(); + + _conn.Close(); + + return true; + } + catch (Exception e) + { + Console.WriteLine("Error when deleting data from database!!"); + Console.WriteLine(e.Message); + return false; + } + finally + { + _conn.Close(); + } } @@ -71,29 +89,51 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess { try { - //conn.Open(); + _conn.Open(); - // Create the SQL command object - //SqlCommand command = new SqlCommand(cmd, conn); + OdbcCommand command = new OdbcCommand(cmd, _conn); + command.ExecuteNonQuery(); - // Execute the command to change the rows in a table - //command.ExecuteNonQuery(); + _conn.Close(); - //conn.Close(); - //conn.Dispose(); return true; } catch (Exception e) { + Console.WriteLine("Error when updating data from database!!"); Console.WriteLine(e.Message); return false; } + finally + { + _conn.Close(); + } } // Very similar to UpdataData if not identical public bool WriteData(string cmd) { - throw new NotImplementedException(); + try + { + _conn.Open(); + + OdbcCommand command = new OdbcCommand(cmd, _conn); + command.ExecuteNonQuery(); + + _conn.Close(); + + return true; + } + catch (Exception e) + { + Console.WriteLine("Error when Writing data from database!!"); + Console.WriteLine(e.Message); + return false; + } + finally + { + _conn.Close(); + } } diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index 72ebc5a..6373ae7 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -43,24 +43,34 @@ namespace TeamHobby.HobbyProjectGenerator.Main IDataSource datasource = new SqlDAO(dbInfo); string sqlQuery = "Select * from log;"; - Object result = datasource.ReadData(sqlQuery); - Console.WriteLine("type of Result: " + result.GetType()); - OdbcDataReader reader = null; + //Object result = datasource.ReadData(sqlQuery); + //Console.WriteLine("type of Result: " + result.GetType()); + //OdbcDataReader reader = null; - if (result.GetType() == typeof(OdbcDataReader)) - { - reader = (OdbcDataReader)result; + //if (result.GetType() == typeof(OdbcDataReader)) + //{ + // reader = (OdbcDataReader)result; - } + //} - Console.WriteLine("Reading from the database"); - while (reader.Read()) - { - Console.WriteLine("Date={0} {1} {2} {3} {4} {5}", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]); - //Console.WriteLine("Col A: {0} ", reader[0]); - //Console.WriteLine("Column: " + reader.FieldCount); - //Console.WriteLine("Column={1}", reader[1]); - } + //Console.WriteLine("Reading from the database"); + //while (reader.Read()) + //{ + // Console.WriteLine("Date={0} {1} {2} {3} {4} {5}", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]); + //} + + // Inserting Data into the database: + //string sqlWrite = "INSERT into log(lvname, catname, userop, logmessage) values " + + // "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');"; + + //Console.WriteLine("Writing to the database... "); + //datasource.WriteData(sqlWrite); + //Console.WriteLine("Writing completed. "); + + string sqlRemove = "DELETE from log where logID = 28;"; + Console.WriteLine("Writing to the database... "); + datasource.WriteData(sqlRemove); + Console.WriteLine("Writing completed. ");