My Favorite Database Wrapper for C#

by Sameera 7. November 2007 19:28

As a guy who started programming back in the days of DAO, I have nothing but love for ADO.NET. But let's face it, you can still end up writing pretty lousy code with it. Not everybody's fully aware of the best uses of the ADO.NET. One of the most common problems I keep stumbling onto in other peoples code is the poor use of ADO.NET Connection Pooling feature.

Somewhere, down the line Microsoft probably started noticing that their cool new data access model wasn't being fully utilized. The birth of the Microsoft Enterprise Library was probably due to this.

As cool as the Enterprise Library is, I've found it to be an over kill for most of my projects. In fact one of my favorite projects featured on CodeProject, was because Enterprise Library's Offline Application Block was so clunky (and very limited).

Around the same time that I worked on on the SCOAB (Smart Client Offline Application Block), I also wrote a ADO.NET wrapper similar to the Enterprise Library. It pretty much follows the public interface of the Enterprise Library, but it's a very tiny wrapper compared to the enormous DAAB (Data Access Application Block).

You can download the .cs file here: AbstractDatabase.zip (1.88 kb)

This contains a generic abstract base class called AbstractDatabase. You can extend this class to use it with any type of relational database type. If you take a look at my SQLite Membership Provider, you'll find this class derived to be used with SQLite ADO.NET Wrapper. I have simply called that class Database, and it's code looks something like this:


public class Database : AbstractDatabase<SQLiteConnection, SQLiteCommand, SQLiteDataAdapter>
{
    protected override string  GetConnectionString()
    {
        return string.Format(ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString,
                            HttpContext.Current.Server.MapPath("~/App_Data"));
    }
}

As you see, the only member you need to override is the GetConnectionString() method. This brings up the question as to why I made this abstract. Why not read the connection string value from the web.config? Well, I've done this because I use this class on both web and desktop projects. And in some cases, especially with WinForms applications, the database is SQLite and resides in the application directory. When that happens, I can always hard code the database file name.

Here's a example of the the Database class's usage (taken from the SQLiteBlogProvier class).


using (Database db = new Database())
    using (DbTransaction txn = db.BeginTransaction())
    {
        DbCommand cmd = db.GetSqlStringCommand("DELETE FROM be_Settings"); // SQLite doesn't support TRUNCATE
        db.ExecuteNonQuery(cmd, txn);

        cmd = db.GetSqlStringCommand(
            "INSERT INTO be_Settings (SettingName, SettingValue) VALUES (@name, @value)");
        db.AddInParameter(cmd, "@name", DbType.String, "");
        db.AddInParameter(cmd, "@value", DbType.String, "");
        foreach (string key in settings.Keys)
        {
            cmd.Parameters[0].Value = key;
            cmd.Parameters[1].Value = settings[key];
            cmd.ExecuteNonQuery();
        }
        txn.Commit();
    }

A quick rundown of the public interface of AbstractDatabase class:


void AddInParameter(System.Data.Common.DbCommand, string, System.Data.DbType, object)
void AddInParameter(System.Data.Common.DbCommand, string, System.Data.DbType, int, object)
DbTransaction BeginTransaction()
void Dispose()
DataSet ExecuteDataSet(System.Data.Common.DbCommand)
int ExecuteNonQuery(System.Data.Common.DbCommand)
ExecuteNonQuery(System.Data.Common.DbCommand, System.Data.Common.DbTransaction)
DbReader ExecuteReader(System.Data.Common.DbCommand)
ExecuteReader(System.Data.Common.DbCommand, System.Data.CommandBehavior)
T ExecuteScalar<T>(System.Data.Common.DbCommand, T)
abstract string GetConnectionString()
DbCommand GetSqlStringCommand(string)
DbCommand GetSqlStringCommand(string, params object[])
DbCommand GetStoredProcedureCommand(string)
Connection { get; }

Update (June 20, 2008): I have updated this class with a minor bug fix and have added support for other parmeter types including Output and InputOuput parameters (Thanks Yordan).

Update (Oct 20, 2008): This wrapper has been updated and made a part of the Codoxide Common Library

Comments

3/12/2008 10:09:48 PM #

Yordan Georgiev

Hi this post rocks!!!

You should put a donate paypal link to it ; )

How to get a dataset from stored procedure using the AbstractDatabase wrapper:
Obviously I am doing something wrong :

public bool Meta_Data_Get ( ref string msg, ref DataSet ds, ref string dbObjectName , ref bool visible , ref string tableName )
       {
       try
           {
           using (Database db = new Database ( ))
           using (DbTransaction txn = db.BeginTransaction ( ))
               {   //comm -- so existing users are overwritten !!!

               string storedProcedureName;
               DbCommand cmd;
               storedProcedureName = "Meta_Data_Get";

               cmd = db.GetStoredProcedureCommand ( storedProcedureName );
              
               db.AddInParameter ( cmd, "@Name", DbType.String, dbObjectName );
               db.AddInParameter ( cmd, "@Visible", DbType.Boolean, visible );
               db.AddInParameter ( cmd, "@Deftable", DbType.String, tableName );

               Utils.Debugger.WriteIf ( "My stored procedure text is " + cmd.CommandText );
              


               cmd.Transaction = txn;
               ds = db.ExecuteDataSet ( cmd );

              
               msg = "The data updated successfully !";
               return true;
               } //eof using
           } //eof try
       catch (Exception e)
           {
           Utils.Debugger.WriteIf ( "The following Exception occured : \n" + e.Message );
           msg = "An error in the application occurred. Report to advanced support error_code: mm1 " + e.Message;
           return false;
           } //eof catch

Yordan Georgiev

3/12/2008 10:23:25 PM #

Yordan Georgiev

Sorry , this was the way to do it ... I had error in other parts of my code ... So put this pay pal link anyway . Thanks !!!

Yordan Georgiev

3/20/2008 12:43:46 AM #

Yordan Georgiev


        public DbParameter AddInOutParameter ( DbCommand cmd, string paramName, DbType paramType, int size, object value )
            {
            DbParameter param = cmd.CreateParameter ( );
            param.DbType = paramType;
            param.ParameterName = paramName;
            param.Size = size;
            param.Value = value;
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add ( param );
            return param;
            }


        public DbParameter AddInOutParameter ( DbCommand cmd, string paramName, DbType paramType, object value )
            {
            DbParameter param = cmd.CreateParameter ( );
            param.DbType = paramType;
            param.ParameterName = paramName;
            param.Value = value;
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add ( param );
            return param;
            }

Yordan Georgiev

3/31/2008 3:08:29 PM #

Sameera

Yordan,
Hey, I'm really sorry I couldn't reply to your comment. BlogEngine has apparently stopped sending me comment notifications. I need to find out why.

Yes, I was intending on including a method for adding an output parameter as well. Somehow, have missed it. I'll update as soon as my laptop comes back from repair... which should be any day now.

Thanks for the PayPal idea. I really want to post bit more code before I considered a step like that Smile
Don't want to seem too gready Laughing

Sameera

4/26/2008 9:43:17 PM #

Andre Tagesgeld

Hi,
nice post - wasn´t an applicable solution for my needs, but pointed me on the right way. Thanks.

Andre Tagesgeld

2/3/2009 10:46:06 PM #

dkhaws7

I tried using the AbstractDatabase class for an Oracle connection, but I get the following error.

Error  1  The type 'Oracle.DataAccess.Client.OracleCommand' cannot be used as type parameter 'COMMAND_TYPE' in the generic type or method 'SERP.core.Providers.AbstractDatabase<CONNECTION_TYPE,COMMAND_TYPE,ADAPTER_TYPE>'. There is no implicit reference conversion from 'Oracle.DataAccess.Client.OracleCommand' to 'System.Data.Common.DbCommand'.  C:\Documents and Settings\dhaws\My Documents\Visual Studio 2008\Projects\SERP\SERP\Providers\OracleProvider\Database.cs  13  18  SERP.core


The code looks like this:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Oracle.DataAccess.Client;

namespace App.core.Providers.OracleProvider
{
    /// <summary>Wrapper for Oracle Database interations</summary>
    public class Database : AbstractDatabase<OracleConnection, OracleCommand, OracleDataAdapter>
    {
...
}

I tried using your BlogEngine implementation as an example.  I am very new to C#, but would like to implement a database access methodology that is db platform independent; however, Oracle is one of the providers I would like to use.

dkhaws7

2/4/2009 5:43:58 PM #

sameera

Hi dkhaws7,

I'm not sure what Oracle.DataAccess.Client.OracleCommand is. But the following code will work. I have to admit that I haven't tried this with Oracle and have almost no experience with any flavor of Oracle. But as long as the database client library conforms to ADO.NET specification, the wrapper will work. System.Data.OracleClient which is bundled with .NET framework does conform to it.

using System.Data.OracleClient;

namespace Codoxide.Data.Samples
{
  class Program
  {
    class OracleDb : Database<OracleConnection, OracleCommand, OracleDataAdapter>
    {
..

sameera

2/5/2009 1:29:01 PM #

dkhaws7

Thanks, that cleared the errors.  I got Oracle.DataAccess.Client from another example.  It worked for the example, but did not work for your implementation.  Thank you again.

dkhaws7

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



About Me

Sameera Perera

Sameera Perera

  • Solutions Architect
  • View Sameera Perera's profile on LinkedIn

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer

This is a personal blog. The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

All forms of source code published on Codoxide.com are distributed under the Apache License, Version 2.0 unless otherwise stated.
The rest of the content are published under a Creative Commons Attribution 3.0 License.
Creative Commons License