C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

Common Database Class for .Net Application (C#)


Posted Date: 10 Sep 2008    Resource Type: Code Snippets    Category: ADO.NET
Author: Shivashankar ChincholiMember Level: Gold    
Rating: 1 out of 5Points: 10



This is uses for common database class for your .Net applications, in this I have implemented IDisposable, Coding standards..


public class DataBaseClass : IDisposable
{
private IntPtr nativeResource = Marshal.AllocHGlobal(100);
private OleDbConnection connection;
private OleDbCommand command;
private OleDbDataAdapter adapter;
private OleDbTransaction trans;
private OleDbCommandBuilder builder;
private DataSet dataSet;
private CommonDTO commonDTO;
private int check;

public DataBaseClass()
{
try
{
string connectionstring = ConfigurationManager.AppSettings ["CONN"];
connection = new OleDbConnection(connectionstring);
if (connection.State == ConnectionState.Closed)
connection.Open();
}
catch (ConfigurationErrorsException ex)
{
throw ex;
}
catch (OleDbException ex)
{
throw ex;
}
catch (InvalidOperationException ex)
{
throw ex;
}
}


public DataSet SelectFromDataBase(string SelectQuery, string tblTablename)
{
try
{
dataSet = new DataSet();
command = new OleDbCommand(SelectQuery, connection);
adapter = new OleDbDataAdapter(command);
trans = connection.BeginTransaction();
command.Transaction = trans;
check = adapter.Fill(dataSet, tblTablename);
trans.Commit();
}
catch (InvalidOperationException ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
catch (SystemException ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
catch (Exception ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
finally
{
if (connection != null)
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
Dispose(true);
}
return dataSet;
}

internal int DbInsert(string Insertquery)
{
check = DbOperation(Insertquery);
return check;
}

internal int DbUpdate(string Updatequery)
{
check = DbOperation(Updatequery);
return check;
}

internal int DbDelete(string Deletequery)
{
check = DbOperation(Deletequery);
return check;
}

public int DbOperation(string query)
{
try
{
command = new OleDbCommand(query, connection);
trans = connection.BeginTransaction();
command.Transaction = trans;
check = command.ExecuteNonQuery();
trans.Commit();
}
catch (InvalidOperationException ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
catch (SystemException ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
catch (Exception ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
finally
{
if (connection != null)
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
Dispose(true);
}
return check;
}

internal int InsertDataInOneTime(DataSet report, string tableName)
{
try
{
command = new OleDbCommand(Resource.SelectQuery + tableName, connection);
adapter = new OleDbDataAdapter(command);
builder = new OleDbCommandBuilder(adapter);
trans = connection.BeginTransaction();
command.Transaction = trans;
adapter.InsertCommand = builder.GetInsertCommand();
check = adapter.Update(report, tableName);
trans.Commit();
}
catch (InvalidOperationException ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
catch (SystemException ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
catch (Exception ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
finally
{
if (connection != null)
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
Dispose(true);
}
return check;
}


public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~DataBaseClass()
{
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
if (connection != null)
{
connection.Dispose();
connection = null;
}
if (trans != null)
{
trans.Dispose();
trans = null;
}
if (command != null)
{
command.Dispose();
command = null;
}
if (adapter != null)
{
adapter.Dispose();
adapter = null;
}
if (builder != null)
{
builder.Dispose();
builder = null;
}
}
// free native resources if there are any.
if (nativeResource != IntPtr.Zero)
{
Marshal.FreeHGlobal(nativeResource);
nativeResource = IntPtr.Zero;
}
}
}

			


Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Common Database class  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Stored Procedure
Previous Resource: Common database class for .Net application (VB.Net)
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use