How to Create Data Access Class

As a software developer our lives revolve around working with data. We create databases to store the data, code to retrieve and modify it. and input interfaces to collect and summarize it. Most of the database applications architecture compose of a Data Access Layer (DAL) using typed data-set, Buisness logic layer (BLL) that organizes custom business rules and Presentation layer composed of forms and pages that shares common layout.

This simple post will guide how to create the Data-Access class that rest of BLL will use to get the data from database.

To create the Data Access class add a new C# file and save it to “App_Code” folder, add the name space of SqlClient at top, and class will be just like this.

using System;
using System.Data.SqlClient;

// Summary
//Data Access Class From Farhanjee.com
//summary
namespace MyData
{
public class DataAccess
{

public DataAccess()
{

}
}
}

Now within a class create a SqlConnection object that will be declare at class level, after that add two method OpenConnection() with return type boolean and CloseConnection(), first method will establish the connection with database and for this get the connection string from webconfig. We may use another method to provide the connection string. second method will close the connection. After that over all class will be look like this.

using System;
using System.Data.SqlClient;
//

// Data Access Class From Farhanjee.com
//

 

namespace MyData
{
public class DataAccess
{
protected SqlConnection Cn = null;

public DataAccess()
{
Cn = new SqlConnection();
}
public bool OpenConnection()
{
string strConString;
bool cnopen = false;
strConString = System.Configuration.ConfigurationManager. ConnectionStrings["MyDBConnection"].ConnectionString;

try
{
if (Cn.State == ConnectionState.Closed)
{
Cn = new SqlConnection(strConString);
Cn.Open();
cnopen = true;
}
}
catch
{
cnopen = flase;
}
return cnopen;
}
public void CloseConnection()
{
Cn.Close();
}
}
}

Add a simple method call it ExecuteNonQuery() with a string parameter, that will execute DML (Data manipulation language) statement.

public bool ExecuteNonQuery(string Qry)
{
OpenConnection();
SqlCommand Cmd = new SqlCommand(Qry.Replace(“True”, “1″).Replace(“False”, “0″), Cn);
bool flag = false;

try
{
Cmd.ExecuteNonQuery();
flag = true;
}
catch (Exception ex)
{
flag = false;
}
finally
{
CloseConnection();
}
return flag;

}

30. October 2011 by Farhan Alam
Categories: ASP.NET, Data Access Class | Tags: | 8 comments

Comments (8)

  1. It’s a nice blog you have over here! It’s very usefull information for me and I just want to thank you for that! If you post more threads as this one, I’ll follow your blog active!

    • that, allow me inform you just what etlcxay did work. The writing is actually quite engaging and that is most likely why I am making an effort in order to comment. I do not make it a regular habit of doing that. Secondly, even though I can see a jumps in reason you make, I am not necessarily convinced of how you appear to connect your ideas which make your final result. For right now I will, no doubt yield to your position but hope in the foreseeable future you actually connect your facts much better.

  2. Appreciation pro this article. Present are categorically tips participating in at this juncture to I choice benefit.
    Android Phones

  3. Very well written post. It will be valuable to anybody who utilizes it, as well as myself. Keep doing what you are doing – for sure i will check out more posts.

  4. I look forward to reading more of your articles and posts in the future, so I’ve bookmarked your blog. When I see good quality content, I like to share it with others. So I’ve created a backlink to your site. Thank you!…

  5. There are some attention-grabbing deadlines on this article however I don’t know if I see all of them center to heart. There’s some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more!

  6. I like your fantastic web site, I was searching for this all over.
    best regards
    Ron

  7. Pingback: les musik

Leave a Reply

Required fields are marked *

*