Click here to Skip to main content
15,922,894 members
Articles / Programming Languages / C#
Article

Easier Data Access

Rate me:
Please Sign up or sign in to vote.
1.80/5 (8 votes)
29 Dec 20042 min read 37.1K   619   21   4
The easier way of accessing data using intermidiate classes between basic ADO.NET classes and your own application.

Introduction

The presented classes can be considered as a layer on top of the core Data Access classes. The usage leads to simplicity of design as well as reduction in the amount of code in executing DML queries.

Background

In most business applications which are developed for small organizations, the amount of code is taken by lines of code which are used to access the data in data stores. The problems come out when these code segments are needed to be repeated regularly. As all of us know very well, object oriented systems give us the solution by putting those code segments in classes and using objects of those classes. All I have done is that, I put those classes in a single DLL package and use in all other application developments also.

Using the code

This DLL file consists of two classes, one is called DataProvider whose objects are used to manipulate the data between application and the data store, and the other class is DataProviderException which is used to provide right type of errors in case of any error.

How does it work?

The DataProvider class consists of protected data members used to encapsulate data source information as well as four type of methods used to insert, delete, select and update data. All these four types of method have several overloaded methods used to provide alternatives.

Constructors

C#
public DataProvider() {}

public DataProvider(String ConnectionString) {}
 
public DataProvider(String ConnectionString, String table) {}
 
DataProvider dp = new DataProvider (
  "@"Data Source=INTERFACEMIRROR;Integrated Security=SSPI;Packet Size=4096;
   Initial Catalog=Northwind;Provider='SQLOLEDB.1';
   Workstation ID=INTERFACEMIRROR"");

dp.Table="Customer";

The main point that has to be noted down is that, before using objects of this class, two issues has to be solved. The first issue is connection string has to be provided through constructor or public property ConnectionString. The second issue is that the table name has to be assigned in a similar fashion.

Public Methods

C#
public void ExecuteSingleRowInsert (String [] values){}

As the sample example, I perform all operation on the customer table of NorthWind data source.

C#
String [] values = 
    {"CustomerID='JDBC2'"  ,"CompanyName='JANDOST'", 
    "ContactName='Jandost'" ,"ContactTitle='Mr'", "Address='******'",
    "City='karachi'", "Region='SOUTH ASIA'","PostalCode='75290'",
    "Country='Pakistan'","Phone='3653358'","Fax='02122510'"};

or in general fashion:

C#
String [] values = {" Column = Value"};

If value is String, it has to be within ‘ ’.

C#
dp.ExecuteSingleRowInsert (values);
  
public void ExecuteMultipleRowsInsert (String [][] vals){}

This method is used to insert more than one row in table at once. For this purpose, all you have to do is just create a two dimensional array, as the above example, in which a row holds a single record and a column holds different records to be inserted.

C#
public void ExecuteDelete (){}

This method will delete all data within the table. For deleting records, you have to use its overloaded method:

C#
public void ExecuteDelete (String cond){}

like

C#
dp.ExecuteDelete ("CustomerId=’JDBC2’");

or

C#
dp.ExecuteDelete ("CustomerId=’JDBC2’ AND Fax=’210254’");
C#
public void ExecuteUpdate (string[] colums ,string condition){}

String [] vals = {"ColumnName = New Value","","", …};

E.g.

C#
String [] vals = {"fax=66666","phoneno=22222"};
dp.ExecuteUpdate (vals , "CustomerId=’JDBC2’");
C#
public ArrayList ExecuteSelect (string [] fields){}
public ArrayList ExecuteSelect (string [] fields, String cond){}

This method can be used in three different ways:

C#
String [] fields = {"*"}; for all Rows Selection

String [] fields = {"Column Name1","Columns Name2"…};

For few columns selection,

C#
ArrayList l =  dp.ExecuteSelect ( fields );
ArrayList l = dp.ExecuteSelect ( fields , "CustomerId=’JDBC2’");

are two verities of this method call.

Note that, the return is an ArrayList which wraps every row of selected table as an object array in itself. For example, suppose your query returns five tuples of the table, each tuple with four fields. The values can be obtained by:

C#
ArrayList al = dp.ExecuteSelect (vals);

Object [] o = null ;
for ( int i = 0 ; i < al.Count ; ++i )
{
   o = (Object []) al [i] ;
   Console.WriteLine (o[0] + o[1] + o[2] + o[3]);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
United Arab Emirates United Arab Emirates
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralInsert Record in Access with C# Pin
Jenga_D21-Dec-06 2:28
Jenga_D21-Dec-06 2:28 
GeneralRe: Insert Record in Access with C# Pin
interface Mirror21-Dec-06 2:34
interface Mirror21-Dec-06 2:34 
GeneralRe: Insert Record in Access with C# Pin
Jenga_D22-Dec-06 0:58
Jenga_D22-Dec-06 0:58 
Questionhow to use repeater control with dataprovider class Pin
rizwan_rashid15-Mar-06 10:47
rizwan_rashid15-Mar-06 10:47 
hi! i m using ur DataProvider class.i m facing difficulty in using it with repeater control.can u give me simple example how to configure ur class with repeater by using select statement. thank you

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.