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");
|
Console.WriteLine("Connection established");
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
//conn = null;
|
//_conn = null;
|
||||||
Console.WriteLine("Error when creating a connection");
|
Console.WriteLine("Error when creating a connection");
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter and setter for Odbc
|
// Getter and setter for Odbc
|
||||||
public OdbcConnection Connection { get; set; }
|
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)
|
public Object? ReadData(string cmd)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_conn.Open();
|
_conn.Open();
|
||||||
OdbcCommand command = new OdbcCommand(cmd, _conn);
|
OdbcCommand command = new OdbcCommand(cmd, _conn);
|
||||||
|
|
||||||
OdbcDataReader reader = command.ExecuteReader();
|
OdbcDataReader reader = command.ExecuteReader();
|
||||||
|
|
||||||
// Execute the command to query the data
|
//_conn.Close();
|
||||||
//using SqlDataReader sqlReader = command.ExecuteReader();
|
|
||||||
|
|
||||||
// while (myReader)
|
|
||||||
// Print to console
|
|
||||||
//conn.Close();
|
|
||||||
//conn.Dispose();
|
|
||||||
//return sqlReader;
|
|
||||||
return reader;
|
return reader;
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("Error when Reading data from databse.");
|
||||||
Console.WriteLine(e.Message);
|
Console.WriteLine(e.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
//finally
|
||||||
|
//{
|
||||||
|
// _conn.Close();
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The also Identical to update data, maybe only one method is enough
|
// The also Identical to update data, maybe only one method is enough
|
||||||
public bool DeleteData(string cmd)
|
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
|
try
|
||||||
{
|
{
|
||||||
//conn.Open();
|
_conn.Open();
|
||||||
|
|
||||||
// Create the SQL command object
|
OdbcCommand command = new OdbcCommand(cmd, _conn);
|
||||||
//SqlCommand command = new SqlCommand(cmd, conn);
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
// Execute the command to change the rows in a table
|
_conn.Close();
|
||||||
//command.ExecuteNonQuery();
|
|
||||||
|
|
||||||
//conn.Close();
|
|
||||||
//conn.Dispose();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("Error when updating data from database!!");
|
||||||
Console.WriteLine(e.Message);
|
Console.WriteLine(e.Message);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_conn.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Very similar to UpdataData if not identical
|
// Very similar to UpdataData if not identical
|
||||||
public bool WriteData(string cmd)
|
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);
|
IDataSource<string> datasource = new SqlDAO(dbInfo);
|
||||||
|
|
||||||
string sqlQuery = "Select * from log;";
|
string sqlQuery = "Select * from log;";
|
||||||
Object result = datasource.ReadData(sqlQuery);
|
//Object result = datasource.ReadData(sqlQuery);
|
||||||
Console.WriteLine("type of Result: " + result.GetType());
|
//Console.WriteLine("type of Result: " + result.GetType());
|
||||||
OdbcDataReader reader = null;
|
//OdbcDataReader reader = null;
|
||||||
|
|
||||||
if (result.GetType() == typeof(OdbcDataReader))
|
//if (result.GetType() == typeof(OdbcDataReader))
|
||||||
{
|
//{
|
||||||
reader = (OdbcDataReader)result;
|
// reader = (OdbcDataReader)result;
|
||||||
|
|
||||||
}
|
//}
|
||||||
|
|
||||||
Console.WriteLine("Reading from the database");
|
//Console.WriteLine("Reading from the database");
|
||||||
while (reader.Read())
|
//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("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]);
|
// 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