Click here to Skip to main content
Licence 
First Posted 29 Dec 2004
Views 29,843
Bookmarked 22 times

Easier Data Access

By | 29 Dec 2004 | Article
The easier way of accessing data using intermidiate classes between basic ADO.NET classes and your own application.
 
Part of The SQL Zone sponsored by
See Also

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

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

public void ExecuteSingleRowInsert (String [] values){}

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

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:

String [] values = {“ Column = Value”};

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

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.

public void ExecuteDelete (){}

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

public void ExecuteDelete (String cond){}

like

dp.ExecuteDelete (“CustomerId=’JDBC2’”);

or

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

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

E.g.

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

This method can be used in three different ways:

String [] fields = {“*”}; for all Rows Selection

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

For few columns selection,

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:

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

About the Author

interface Mirror

Team Leader

United Arab Emirates United Arab Emirates

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralInsert Record in Access with C# PinmemberJenga_D2:28 21 Dec '06  
GeneralRe: Insert Record in Access with C# Pinmemberinterface Mirror2:34 21 Dec '06  
hello
 
if you are using Dataset for your dataAccess. then you need to insert Data into Dataset first then just call Update method of DataAdaptor. it would insert values to your data store.
 
i hope it works for you.
 
Kindest Regards,
GeneralRe: Insert Record in Access with C# PinmemberJenga_D0:58 22 Dec '06  
Questionhow to use repeater control with dataprovider class Pinmemberrizwan_rashid10:47 15 Mar '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 30 Dec 2004
Article Copyright 2004 by interface Mirror
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid