A class with methods to execute a query, execute stored procedures, execute scalar query, basically all database operations for an Oracle and Microsoft databases. Currently I have one class with different methods for each operation, but there is a lot of repeat code. How to design this in a proper object oriented way?
Here is what I have currently: I have similar method like execute for execute scalar that returns a string, execute scalar that returns an int, etc.
public class DBoperations
{
private string querystringval;
private string logfileloc;
private string connectionstringval;
LogToFile logobj = new LogToFile();
SqlConnection SqlConn = new SqlConnection();
public string logfilelocval
{
get { return logfileloc; }
set { logfileloc = value; }
}
public string queryValue
{
get { return querystringval; }
set { querystringval = value; }
}
public string connectionvalue
{
get { return connectionstringval; }
set { connectionstringval = value; }
}
public Boolean connecttodb()
{
logobj.fileName = logfilelocval;
//SqlConnection SqlConn = new SqlConnection(connectionvalue);
SqlConn.ConnectionString = connectionvalue;
try
{
SqlConn.Open();
logobj.MyLogFile("**SUCCESS** ", "Database connection opened");
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
logobj.MyLogFile("**FAILURE**", "Database Connection Failed" + e.Message);
return false;
throw new IndexOutOfRangeException();
}
}
public string executeaquery()
{
try
{
SqlCommand querystring = new SqlCommand(queryValue, SqlConn);
querystring.CommandTimeout = 90;
querystring.ExecuteNonQuery();
logobj.MyLogFile("**SUCCESS** ", "Query Executed Successfully.");
querystring.Dispose();
return "True";
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
logobj.MyLogFile("**FAILURE**", "Query did not execute." + e.Message);
return e.Message;
throw new IndexOutOfRangeException();
}
}
}}
WheelReinventedSqlConnection,SqlCommand, etc, insufficient? What does this new class gain for code that's going to use it?