Skip to content

Commit

Permalink
Added a DBConnector class
Browse files Browse the repository at this point in the history
This was a class I wrote few weeks back, when I was trying to create a
DBConnectionFactory. Most of the code in the class has been gleaned from
various sources on the internet.
  • Loading branch information
sqlchow committed Oct 4, 2014
1 parent 7ca0027 commit ee90067
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 19 deletions.
6 changes: 0 additions & 6 deletions TrackMyQuery/CXPacketTrackingForm.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;

namespace TrackMyQuery
{
Expand Down
245 changes: 245 additions & 0 deletions TrackMyQuery/DbConnector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
using System;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;

//@sqlchow: This class is a sum of all the research I was doing on creating a dbconnection factory.

namespace TrackMyQuery
{
class DbConnector : IDisposable
{
private string strConnectionString;
private DbConnection dbConnection;
private DbCommand dbCommand;
private DbProviderFactory dbFactory = null;
private bool boolHandleErrors;
private string strLastError;
private bool boolLogError;
private string strLogFile;

public DbConnector(string connectionstring, Providers provider)
{
strConnectionString = connectionstring;
switch (provider)
{
case Providers.SqlServer:
dbFactory = SqlClientFactory.Instance;
break;
case Providers.OleDb:
dbFactory = OleDbFactory.Instance;
break;
case Providers.ODBC:
dbFactory = OdbcFactory.Instance;
break;
}

dbConnection = dbFactory.CreateConnection();
dbCommand = dbFactory.CreateCommand();

dbConnection.ConnectionString = strConnectionString;
dbCommand.Connection = dbConnection;
}

public DbConnector(string strConnectionString)
: this(strConnectionString, Providers.SqlServer)
{
}

public bool HandleErrors
{
get { return boolHandleErrors; }
set { boolHandleErrors = value; }
}

public string LastError
{
get { return strLastError; }
}

public bool LogErrors
{
get { return boolLogError; }
set { boolLogError = value; }
}

public string LogFile
{
get { return strLogFile; }
set { strLogFile = value; }
}

public int AddParameter(string name, object value)
{
DbParameter dbParam = dbFactory.CreateParameter();
dbParam.ParameterName = name;
dbParam.Value = value;

return dbCommand.Parameters.Add(dbParam);
}

public int AddParameter(DbParameter dbParam)
{
return dbCommand.Parameters.Add(dbParam);
}

public DbCommand Command
{
get { return dbCommand; }
}

public DbDataReader ExecuteReader(string dbQuery, CommandType dbCommandType, ConnectionState dbConnectionState)
{
DbDataReader dbReader = null;

dbCommand.CommandText = dbQuery;
dbCommand.CommandType = dbCommandType;

try
{
if (dbConnection.State == System.Data.ConnectionState.Closed)
{
dbConnection.Open();
}

if (dbConnectionState == ConnectionState.CloseOnExit)
{
dbReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
else
{
dbReader = dbCommand.ExecuteReader();
}
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
dbCommand.Parameters.Clear();
}

return dbReader;
}


public DataSet ExecuteDataSet(string dbQuery, CommandType dbCommandType, ConnectionState dbConnectionState)
{
DataSet dbDataSet = new DataSet();
DbDataAdapter dbAdapter = dbFactory.CreateDataAdapter();

dbCommand.CommandText = dbQuery;
dbCommand.CommandType = dbCommandType;
dbAdapter.SelectCommand = dbCommand;

try
{
dbAdapter.Fill(dbDataSet);
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
dbCommand.Parameters.Clear();
if (dbConnectionState == ConnectionState.CloseOnExit)
{
if (dbConnection.State == System.Data.ConnectionState.Open)
{
dbConnection.Close();
}
}
}

return dbDataSet;
}

public object ExecuteScalar(string dbQuery, CommandType dbCommandType, ConnectionState dbConnectionState)
{
dbCommand.CommandText = dbQuery;
dbCommand.CommandType = dbCommandType;
object returnObj = null;
try
{
if (dbConnection.State == System.Data.ConnectionState.Closed)
{
dbConnection.Open();
}
returnObj = dbCommand.ExecuteScalar();
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
dbCommand.Parameters.Clear();
if (dbConnectionState == ConnectionState.CloseOnExit)
{
dbConnection.Close();
}
}

return returnObj;
}

private void HandleExceptions(Exception ex)
{
StackFrame exStack = new StackTrace(ex, true).GetFrame(0);
string exceptionMethod = exStack.GetMethod().Name;
string exceptionLine = exStack.GetFileLineNumber().ToString();
string exSource = string.Format("Exception raised in {0} at line number: {1}", exceptionMethod, exceptionLine);

if (LogErrors)
{
WriteToLog(exceptionMethod, exceptionLine, ex.Message);
}

if (HandleErrors)
{
strLastError = exSource + "\n" + ex.Message;
}
else
{
throw ex;
}
}

private void WriteToLog(string methodName, string lineNumber, string message)
{
StreamWriter logWriter = File.AppendText(LogFile);
logWriter.WriteLine(DateTime.Now.ToString() + " - [" + methodName + ":" + lineNumber + "] - " + message);
logWriter.Close();
}

private void WriteToLog(string message)
{
StreamWriter logWriter = File.AppendText(LogFile);
logWriter.WriteLine(DateTime.Now.ToString() + " - " + message);
logWriter.Close();
}

public void Dispose()
{
dbCommand.Dispose();
dbConnection.Close();
dbConnection.Dispose();
}
}

public enum Providers
{
SqlServer, ODBC, OleDb
}

public enum ConnectionState
{
KeepOpen, CloseOnExit
}
}
7 changes: 0 additions & 7 deletions TrackMyQuery/MainMenuForm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

Expand Down
6 changes: 0 additions & 6 deletions TrackMyQuery/QueryProfilesTrackingForm.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;

namespace TrackMyQuery
{
Expand Down
1 change: 1 addition & 0 deletions TrackMyQuery/TrackMyQuery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="CXPacketTrackingForm.Designer.cs">
<DependentUpon>CXPacketTrackingForm.cs</DependentUpon>
</Compile>
<Compile Include="DbConnector.cs" />
<Compile Include="MainMenuForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down

0 comments on commit ee90067

Please sign in to comment.