Add writing, deleting data to SqlDAO
This commit is contained in:
parent
d76a389b9e
commit
f8bafc8b6d
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -43,24 +43,34 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
||||
IDataSource<string> 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. ");
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user