Transparent Database Access with ADO.NET
by Nick Harrison12/16/2002
The Dream
The dream of transparent data access is to not have to care about where the data is coming from. You have more important things to worry about, like providing a nice user interface and getting the business logic right on time. Ideally, it should not matter if the data is coming from SQL Server, Oracle, Outlook, or anywhere else useful data may be stored. It should not matter if you choose to develop against Access at home, SQL Server in development, and use Oracle in production. In a perfect world, we shouldn't have to rewrite our code in order to use a more efficient data access provider, but we do have to work for this dream.
The Problem
Unfortunately, data sources are different and need to be treated differently. What works well for one data source may not necessarily work well for another, and coding to the lowest common denominator in database functionality does not allow us to take advantage of the strengths of any one platform. This causes our code to be less efficient than it needs to be.
In DotNet, we have SqlClient, OracleClient, and ODP.NET all providing database-specific, optimized classes that try to leverage the strengths of the respective databases and OleDb providing access to everything else through a database-neutral, non-optimized set of classes.
This allows us to have efficient optimized data access if we know that we will be using SQLServer or Oracle all the time, but switching data providers requires changing the data type for every Connection object, every Command object, every Data Reader object, every DataAdapter object, etc. If we are not using one of these two platforms, or not committed to using the same one in all settings, we would seem to be doomed to inefficient, non-optimized code. Fortunately, DotNet provides a solution to this problem.
The Solution
If you are not familiar with the object-oriented concept of polymorphism, this solution will endear the concept to you. Polymorphism simply refers to an object behaving differently in different circumstances. If we write our code in terms of a generic "class," and have this class behave like an SQLClient when connecting to an SQLServer Database, an OracleClient when connecting to an Oracle Database, and an OleDb class otherwise, and we won't have to get bogged down in the specifics of which provider we are using.
|
Related Reading
ADO.NET in a Nutshell |
This generic class that we will be using is not really a class in the traditional sense of the word. We will be using the interfaces that all of the data providers implement. We will not delve much into the details of interfaces. For our purposes, think of an interface as a contract between the class and users of that class. The interface defines a set of methods and properties that any class implementing the interface will include. The interface does not implement any of these, so we cannot instantiate an instance of an interface. Instead we typecast an instance of a class that implements the interface to the interface. When we call one of the methods defined in the interface, the original class' implementation is being called without us having to keep track of the type of the original class.
The interfaces we will be concerned with here include IDbConnection, IDbCommand, IDataReader, and IDbDataAdapter. For a complete listing of the mehods and properties for these interfaces, refer to the MSDN documentation: IDbConnection, IDbCommand, IDataReader, and IDbDataAdapter.
Because all of the data providers implement these interfaces, we know that they will all include database-specific versions of the methods we will need to access our data.
The Code
This code is not intended as a guide for best coding practices. Many of the details needed in an enterprise solution are left out so that we can focus on the details of using interfaces to interact with ADO.NET.
We will start by defining a function that will read various settings from the web.config
file to specify such things as the ConnectionString, the Data Type for the Connection
object, and the Assembly containing this data type. To pull settings from the config
file, we must include the
System.Configuration namespace. We will also be using some of
the features of Reflection to dynamically build the base type that is implementing the
interface. To use Reflection, we must include the
System.Reflection namespace.
public static IDbConnection GetConnection (string dbType)
{
The dbType parameter will be concatenated with the Configuration setting name to get the settings
specific to the database type that we are using. In this example, we will be looking
for SQLAssembly or OracleAssembly, etc.:
Type connectionType = null;
IDbConnection newConnection = null;
// Get the assembly that owns the process
Assembly runningAssembly = Assembly.GetExecutingAssembly();
// Get all the assemblies that are referenced
AssemblyName [] referencedAssemblies =
runningAssembly.GetReferencedAssemblies();
// Find the current type of database you need
string strTargetAssembly =
ConfigurationSettings.AppSettings[dbType + "Assembly"];
We will use the currently-executing assembly to get a list of all of the assemblies
referenced by the running app. We will then loop through this array of
AssemblyNames to find the assembly where the config file promised the classes for our
database type could be found.
// Search through all referenced assemblies
foreach (System.Reflection.AssemblyName
currentAssemblyName in referencedAssemblies)
{
When we find the referenced Assembly, we will load it giving us access to the non-static
methods of the
Assembly class. We will use this Assembly instance to get an instance of the
Type object corresponding to the data type for the Connection object.
// Open the assembly is the one we're looking for
if (currentAssemblyName.Name == strTargetAssembly)
{
Assembly referencedAssembly = Assembly.
Load(currentAssemblyName.FullName);
connectionType = referencedAssembly.
GetType(ConfigurationSettings.
AppSettings[dbType + "Connection"]);
// Stop searching through the assemblies
break;
}
}
If we were able to find the referenced assembly and get an instance of the Type for the
data type referenced for the connection object, we will create an instance of this data type
and pass that back as the IDbConnection object. The
Activator object allows to instantiate instances of an class.
if (connectionType != null)
{
// Create a connection of the type
newConnection = (IDbConnection)Activator.
CreateInstance (connectionType);
// Retrieve the Connection String
newConnection.ConnectionString = ConfigurationSettings.
AppSettings[dbType + "ConnectionString"];
}
// Return the new connection
return newConnection;
}
For our purposes, the relevant sections from the config file include:
<appSettings>
<add key="SQLAssembly"
value= "System.Data"
/>
<add key= "SQLConnection"
value= "System.Data.SqlClient.SqlConnection"
/>
<add key= "SQLConnectionString"
value="User ID=sa;
Password=;
Database=Northwind;
Server=localhost;"
/>
<add key="OracleAssembly"
value="System.Data.OracleClient"
/>
<add key="OracleConnection"
value="System.Data.OracleClient.OracleConnection"
/>
<add key="OracleConnectionString"
value="User ID=scott;Password=tiger;Data Source=rddvl"
/>
</appSettings>
We will also define a method that will use our GetConnection method to return an
IDbConnection for the database type specified.
static IDataReader DataReader(string dbType, string query)
{
// Get a connection Object
IDbConnection conn = GetConnection(dbType);
// Create a Command Object
IDbCommand cmd = conn.CreateCommand();
// Set the Query
cmd.CommandText = Queryquery;
// Open the connection
cmd.Open();
// Execute the Command to create a DataReader
IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// Return the DataReader
return rdr;
}
Once the interfaces are connected to instances of the appropriate objects, we can use the methods in the interface without regard for which data type they point to, and rest assured that the appropriate optimized function calls will be made.
To demonstrate using this function, we will define a Web form that includes two data grids side by side. We will display data from Oracle in the first and data from SQLServer in the second:
<body>
<form id="TransparentADOAccess" method="post" runat="server">
<table border="1">
<tr>
<td><asp:DataGrid ID="dgOracle"
Runat="server"></asp:DataGrid></td>
<td><asp:DataGrid ID="dgSQL"
Runat="server"></asp:DataGrid></td>
</tr>
</table>
</form>
</body>
In the Page_Load for this page, we will call the method we defined earlier
twice, to populate the two datagrids with the names of the tables from both
database platforms' demonstation databases.
private void Page_Load(object sender, System.EventArgs e)
{
// Specify the Query
string query = @"SELECT table_name
FROM all_Tables
WHERE owner = 'SCOTT' and rownum < 11";
// Create the DataReader
IDataReader oraDR = DataReader ("Oracle", query);
// Setup DataBinding
dgOracle.DataSource = oraDR;
dgOracle.DataBind();
oraDR.Close();
// Get another
query = @"SELECT top 10 table_name
FROM INFORMATION_SCHEMA.TABLES";
IDataReader sqlDR = DataReader ("SQL", query);
// Bind again
dgSQL.DataSource = sqlDR;
dgSQL.DataBind();
sqlDR.Close();
}
The output produced should look similar to this:
Another Example
Here we will use the same .aspx page defined earlier, and the same GetConnection method,
but we will populate the datagrids using DataAdapters instead of DataReaders.
To use DataAdapters, we will need to define a new function, GetDataAdapter, similiar to the
GetConnection function. We will also extend the web.config file to include the details
about the data type for the DataAdapters.
public static IDbDataAdapter GetDataAdapter(string dbType)
{
Type adapterType = null;
IDbDataAdapter newAdapter = null;
// Like before, get the main assembly
Assembly runningAssembly = Assembly.GetExecutingAssembly();
// Find the referenced assemblies
AssemblyName [] referencedAssemblies =
runningAssembly.GetReferencedAssemblies();
// Get the Configuration setting for the specified
// Assembly
string strTargetAssembly = ConfigurationSettings.
AppSettings[dbType + "assembly"];
// Find the assembly
foreach (System.Reflection.AssemblyName
currentAssemblyName in referencedAssemblies)
{
// If we found the right assembly...
if (currentAssemblyName.Name == strTargetAssembly)
{
// Load the Assembly and the configuration
// information
Assembly newAssembly = Assembly.
Load(currentAssemblyName.FullName);
adapterType = newAssembly.
GetType(ConfigurationSettings.
AppSettings[dbType + "DataAdapter"]);
// Stop searching if we get this far
break;
}
}
// If we found an adapter type,
if (adapterType != null)
{
// Create a new instance of the adapter
newAdapter = (IDbDataAdapter)Activator.
CreateInstance (adapterType);
}
// return it
return newAdapter;
}
The following sections need to be added to the config file:
<add key="SQLDataAdapter"
value = "System.Data.SqlClient.SqlDataAdapter"/>
<add key="OracleDataAdapter"
value = "System.Data.OracleClient.OracleDataAdapter"/>
We will also define a method that will use the GetConnection and GetDataAdapter methods to
return an IDbDataAdapter ready for use.
static IDbDataAdapter DataAdapter(string dbType, string query)
{
// Get a connection for the type we want
IDbConnection conn = GetConnection(dbType);
// Get a new DataAdapter for the type we want
IDbDataAdapter dataAdapter = GetDataAdapter(dbType);
// Create a command from the connection
dataAdapter.SelectCommand = conn.CreateCommand();
// Specify the query
dataAdapter.SelectCommand = query;
// Return the new adapter
return dataAdapter;
}
In the Page_Load, we can now use our new functions and bind the DataSets filled from the
DataAdapters to the DataGrids.
private void Page_Load(object sender, System.EventArgs e)
{
// Create the new DataAdapter
IDbDataAdapter oraDA = DataAdapter ("Oracle",
"select table_name from all_Tables where rownum < 11");
// Create a new DataSet
DataSet dsOra = new DataSet();
// Tell the adapter to fill it
oraDA.Fill (dsOra);
// Do Data Binding
dgOracle.DataSource = dsOra.Tables[0];
dgOracle.DataBind();
// Get another adapter
IDbDataAdapter sqlDA = DataAdapter ("SQL",
"SELECT top 10 table_name FROM INFORMATION_SCHEMA.TABLES");
// Create a new DataSet
DataSet dsSQL = new DataSet();
// Have the DataAdapter fill it
sqlDA.Fill (dsSQL);
// Do more binding
dgSQL.DataSource = dsSQL.Tables[0];
dgSQL.DataBind();
}
Conclusion
Nothing is ever constant. The database used today may not be the database used in the future. The best data provider available for your database of choice today may not be the best data provider to use in the future. This article provides some guidance on how to protect your code from such changes. This simple method can also be expanded to serve as a common data access layer, helping to ensure consistent connection strings, which will help in connection pooling and management.
Nick Harrison UNIX-programmer-turned-.NET-advocate currently working in Charlotte, North Carolina using .NET to solve interesting problems in the mortgage industry.
Return to ONDotnet.com
Showing messages 1 through 20 of 20.
-
SQL Helper
2006-09-14 13:57:03 Netaji [View]
-
Templates
2003-05-08 09:04:22 anonymous2 [View]
Using templates could be the ideal solution to having a generic database access layer, where the desired provider could be one of the template parameters.
Alas, templates are not supported for Managed C++ ;(
Great approach nonetheless!
Yazan Diranieh
www.diranieh.com
-
Can't be done
2003-03-02 18:06:20 anonymous2 [View]
Still can't do this like JDBC. As already pointed out, parameter binding changes between data providers. Another one that will bite you is transactions. In SqlClient, you need to bind the transaction to each command. In ODP.NET command doesn't even have the methods to do this. According to their documentation, transactions are per connection and don't work like SqlClient. If you're doing select * from table where without bound parameters or transactions, you're ok. If not, you still need to recode for each DB type. -
Can't be done
2003-04-30 22:43:29 anonymous2 [View]
yeah, i face the same problem with you. when we use IDbDataAdapter, we will lost many nice OverLoading method of "Fill". we left only IDbDataAdapter.Fill(DataSet)...... :(
that's mean we cant Fill in DataTable, we cant Fill DataTable with a name....etc. -
Can't be done
2003-05-02 09:47:01 anonymous2 [View]
you can get around this by writing your own overloaded versions. (Not great but works)
Ie. MyDBAccess.Fill(IDbDataAdapter aDataAdapter, DataSet aDataSet, string aTableName)
etc and determining the db used inside this function.
works quite well really.
bj
-
Performance?
2003-02-10 09:51:19 neh123us@yahoo.com [View]
The reflection will impact performance. There is a trade off between flexibility and performance. If performance is more important and you know that you will be limited to a small number of different platforms or atleast adding additional platforms more rarely, then there is a less flexible approach that would not rely on reflection.
In the GetConnection and GetDataAdapter methods, rather than searching through each of the referenced for the object type that is referenced in a config file, you could hard code object types against the valid values for the DbType. Using this approach, you may want to convert the DbType to an enumeration. The code would look similiar to this:
public static IDbConnection GetConnection (DBTypes dbType)
{
IDbConnection NewConnection;
if (dbType == DBTypes.SQL)
{
NewConnection = new SqlConnection ();
NewConnection.ConnectionString = ConfigurationSettings. AppSettings["SQLConnectionString"];
}
if (dbType == DBTypes.Oracle)
{
NewConnection = new OracleConnection();
NewConnection.ConnectionString = ConfigurationSettings. AppSettings["OracleConnectionString"];
}
return NewConnection;
}
It is important to always keep in mind which is more important speed, flexibility, maintainability, development time, etc.
I hope this helps,
Nick
-
Performance?
2003-02-06 11:13:00 anonymous2 [View]
I like this approach, but isn't the cost of reflection high?
-
Parameters
2003-02-05 19:41:27 anonymous2 [View]
Oracle and SQLServer data providers have a different way to specify parameter markers in sentences:
select * from customer where customerid = @customerid
vs
select * from customer where customerid = :customerid
Do you have a good way to abstract those differences?
Thanks -
Parameters
2003-06-09 20:00:04 anonymous2 [View]
One way a manage this problem is using String.Format() function. For example:
private const string SQL_GET_RECORD = "SELECT * FROM UserTbl WHERE id = {0} AND name='{1}'";
You can format your string like this:
String.Format(SQL_GET_RECORD_BY_ID,123,'jesse');
This will produce the following string:
SELECT * FROM UserTbl WHERE id = 123 AND name='jesse'
Hope it helps,
Jesse -
Parameters
2003-02-06 10:25:06 neh123us@yahoo.com [View]
A good way to hide such differences in the SQL syntax is to use stored procedures and bind parameters using the IDbDataParameter interface. Another method would be to store the parameter prefix character in a config file, but this would not abstract the differences in join syntax, set operations, and other vendor specific syntax.
I hope this helps.
-
nice article - corrected code
2003-01-22 07:12:46 anonymous2 [View]
its "conn.Open()" not "cmd.Open();" and
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
dataAdapter.SelectCommand = cmd;
will work better than
dataAdapter.SelectCommand = query;
-
Great Article
2003-01-17 06:53:17 anonymous2 [View]
Nick,
Your article was a big help. In our shop we have multiple databases and were looking for a way to make database access more abstract, while leveraging the benefits of the provider specific clients. Thanks for the help.
--Randall
-
Class factories and configuration are good
2003-01-07 07:56:40 mogens [View]
The mechanism used is what's generally known as a class factory (go look in the Gamma et al big book of patterns for more details).
The Pragmatic Programmers book says "Configuration is better than code". All the stuff you can push down into config files instead of hard-coding is good. This code is a great example of that.
At work we've actually taken a different approach, which is to wrap the classfactory and the general code access inside a custom provider, so we just use the custom provider and it figures out which database it's talking to on the fly.
-
Great Article
2002-12-30 20:42:08 anonymous2 [View]
This is a Great article, something we were looking for.Thanks very much
-
runtime error
2002-12-20 17:53:52 anonymous2 [View]
this error refers to the static IDbDataAdapter DataAdapter(string dbType, string query) function
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 100:
Line 101: // Create a Command Object
Line 102: IDbCommand cmd = conn.CreateCommand();
-
runtime error
2002-12-22 18:14:47 neh123us@yahoo.com [View]
Most likely this is caused by the GetConnection method not finding the assembly referenced in the config file. This is probably caused by not referencing the assembly, or possibly the assembly name is misspelled.
Verify that the assembly is in the references collection of the project. Let me know if you continue to have problems.
Nick
-
runtime error
2002-12-23 13:12:17 anonymous2 [View]
Ok. It's working now. Thanks. The assembly name was misspelled. And congratulations for the script. I've seen some aproaches of this kind but yours is the simplest one.
-
Interesting article
2002-12-18 12:37:18 anonymous2 [View]
Hello Nick,
I'm new into .NET and I come from Java camp. I really like what I see in .NET and one of the things that ammused me was ADO.NET and not being able to use the code base for mulitple databases (unless and other wise if we're using ODBC or OLEDB drivers).
This article points me into a different direction and it kind of follows the JDBC approch.
Thanks for the very nice article and Keep'm coming buddy.










We are already having a application which use SQLHelper for MS SQL Server, But Now we are thinking of migrating to Oracle 9i, so according to you how should i manage common SQLHelper to manage DB Oprations.
Please help me on this.