Create a Vendor-Independent Data Access Class

Vendor independent or provider agnostic data access is a relatively easy thing to implement, thanks to the structure of ADO.NET. You’ll find the classes you’d need in the System.Data.Common namespace.

To access data without knowing the provider type, you’d start in your web.config, by adding these entries to it:

<connectionStrings>
                <add name=”theString” connectionString=”your string here” providerName=”System.Data.SqlClient”/>
</connectionStrings>
<appSettings>
                <add key=”select” value=”SELECT * FROM Customers”/>
</appSettings>

Then in your data access class, you should define a DBProviderFactory, create an instance of it by calling the GetFactory static method passing the provider type as parameter (defined in your web.config). Then you should create a DBConnection class and a DBCommand class, or anything else you’d need, and use it as if you’d use for example the SQL classes. Example:

DBProviderFactory fact = DBProviderFactory.GetFactory(WebConfigurationManager.ConnectionStrings[“theString”].ProviderName);
DBConnection theConnection = fact.CreateConnection();
theConnection.ConnectionString = WebConfigurationManager.ConnectionStrings[“theString”].ConnectionString;
DBCommand theCommand = fact.CreateCommand();
theCommand.CommandText = WebConfigurationManager.AppSettings[“select”];
theCommand.Connection = theConnection;
theConnection.Open();
GridView1.DataSource = theCommand.ExecuteReader();           
GridView1.DataBind();
theConnection.Close();

As you can see, creating a provider-agnostic data access class is an easy and straightforward task. Use it regularly to build more flexible applications.

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: