From 012af8f5d791c21a93c538b9b74fd6da4e1cc550 Mon Sep 17 00:00:00 2001 From: Alatreon Date: Sun, 5 Dec 2021 16:09:23 -0800 Subject: [PATCH] Add read method for data access --- .../Contracts/IDataSource.cs | 4 +- .../Implementations/SQLSource.cs | 51 +++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs index f173ba6..c2fcd16 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Contracts/IDataSource.cs @@ -16,9 +16,9 @@ namespace TeamHobby.HobbyProjectGenerator.Archive bool WriteData(string cmd); //Method for deleteing data from a data source, 0 for successful - bool DeleteData(); + bool DeleteData(string cmd); // Method for updating data from a data source, 0 for sucessful - bool UpdateData(); + bool UpdateData(string cmd); } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs index c445c06..0daeab1 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/Implementations/SQLSource.cs @@ -12,31 +12,41 @@ namespace TeamHobby.HobbyProjectGenerator.Archive { public class SQLSource : IDataSource, IRelationArchivable { - //private SqlConnection conn; + private SqlConnection conn; - //public SQLSource(string info) - //{ - // conn = new SqlConnection(info); - //} + public SQLSource(string info) + { + // Do I put using here to make sure the connection closed once the object is gone? + conn = new SqlConnection(info); + // Perhaps open the connection here? + // conn.Open(); + } public bool DeleteData() { throw new NotImplementedException(); } + // Makre sure to Check for instanceof() before casting to a SQLReader in the controller public Object ReadData(string cmd) { try { - // conn.open(); + conn.Open(); + //SqlCommand command = new SqlCommand(null, conn); + SqlCommand command = new SqlCommand(cmd, conn); + // conn.Execute(); Console.WriteLine("Access a SQL database"); Console.WriteLine("Select * from archive"); + // Execute the command to query the data + using SqlDataReader sqlReader = command.ExecuteReader(); + // while (myReader) // Print to console - // conn.close(); - return null; + conn.Close(); + return sqlReader; } catch (Exception e) { @@ -45,7 +55,7 @@ namespace TeamHobby.HobbyProjectGenerator.Archive } } - public bool UpdateData() + public bool UpdateData(string cmd) { throw new NotImplementedException(); } @@ -92,5 +102,28 @@ namespace TeamHobby.HobbyProjectGenerator.Archive } } + public Object ReadPreparedStmt(string table){ + conn.Open(); + //SqlCommand command = new SqlCommand(null, conn); + SqlCommand command = new SqlCommand("SELECT * from @table;", conn); + SqlParameter tableParam = new SqlParameter("@table", System.Data.SqlDbType.Text, 50); + + tableParam.Value = table; + command.Parameters.Add(tableParam); + + // conn.Execute(); + Console.WriteLine("Access a SQL database"); + Console.WriteLine("Select * from archive"); + + // Make the Prepare statement and excute the query: + command.Prepare(); + SqlDataReader sqlReader = command.ExecuteReader(); + + // while (myReader) + // Print to console + // conn.close(); + return sqlReader; + } + } }