Skip to main content

DataGeneral: Lightweight ADO.NET wrapper replacing Enterprise Library Data Access Application Block

 

The DataGeneral class provides access to the most often used features of ADO.NET in a simple-to-use class boosting developer productivity.

DataGeneral is lightweight for maximum performance. It employs Microsoft best practices. DataGeneral provides the best features of Enterprise Libraries Data Application block. In my experience, I like the Enterprise libraries Application Blocks to learn what features of the .NET framework are available , and best practice coding techniques (hence the name patterns and practices). But I don't like the weight of the enterprise libraries, and the random errors I get when versions are out of sync across environments. That is why I wrote DataGeneral which is just a single wrapper class. With DataGeneral you will have less lines of code to debug. DataGeneral is specific to Microsoft Sql Server.

For example, one of the features of the Data Access Block is stated as: "By changing the settings in the configuration file, developers can use their applications with different database configurations without recompiling their code." This makes me laugh: What application can have its most major component ripped out and replaced without recompiling the code? I would state that this feature is useless as are many of the other features in the Data Access block. However Enterprise library has many good features like saving you lines of code when building a SqlCommands with SqlParameters. That's why I wrote DataGeneral: All the good parts without all the useless parts.

DataGeneral has been used in several enterprise applications currently in production. On each project it has been further refined and perfected.

 

Example 1) DataGeneral Source Code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
public class DataGeneral
{

private static SqlConnection createConnection()
{
string sqlCn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection cn = new SqlConnection(sqlCn);
cn.Open();
return cn;
}

/// <summary>
/// Wraps the SqlCommand.ExecuteReader() method.
/// </summary>
/// <param name="procedureNameOrSql">Stored procedure name or a Sql statement.</param>
/// <param name="parameters">List of SqlParameter. Set to null if no parameters.</param>
/// <param name="isStoredProcedure">True if the procedureNameOrSql is a stored procedure, false if it is a SQL statement.</param>
/// <returns>Returns a SqlDataReader which MUST be wrapped in a using statement so that its SqlConnecion is closed as soon as the SqlDataReader is disposed.</returns>
public static SqlDataReader ExecuteReader(string procedureNameOrSql, List<SqlParameter> parameters, bool isStoredProcedure)
{
//IMPORTANT: make sure you wrap the returned SqlDataReader in a using statement so that it is closed. (You do not need to close the SqlConnection object.)
SqlConnection cn = createConnection();

SqlCommand cmd = new SqlCommand(procedureNameOrSql, cn);

if (isStoredProcedure)
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

/// <summary>
/// Wraps the SqlCommand.ExecuteScalar() method.
/// </summary>
/// <param name="procedureNameOrSql">Stored procedure name or a Sql statement.</param>
/// <param name="parameters">List of SqlParameter. Set to null if no parameters.</param>
/// <param name="isStoredProcedure">True if the procedureNameOrSql is a stored procedure, false if it is a SQL statement.</param>
/// <returns>Returns the first value of the first row of the Sql Statement.</returns>
public static object ExecuteScalar(string procedureNameOrSql, List<SqlParameter> parameters, bool isStoredProcedure)
{
object scalarValue;
using (SqlConnection cn = createConnection())
{
SqlCommand cmd = new SqlCommand(procedureNameOrSql, cn);
if (isStoredProcedure)
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
scalarValue = cmd.ExecuteScalar();
}
return scalarValue;
}

/// <summary>
/// Wraps the SqlCommand.ExecuteNonQuery() method.
/// </summary>
/// <param name="procedureNameOrSql">Stored procedure name or a Sql statement.</param>
/// <param name="parameters">List of SqlParameter. Set to null if no parameters.</param>
/// <param name="isStoredProcedure">True if the procedureNameOrSql is a stored procedure, false if it is a SQL statement</param>
/// <returns>Returns the number of rows affected by ExecuteNonQuery().</returns>
public static int ExecuteNonQuery(string procedureNameOrSql, List<SqlParameter> parameters, bool isStoredProcedure)
{
int rowsAffected;
using (SqlConnection cn = createConnection())
{
SqlCommand cmd = new SqlCommand(procedureNameOrSql, cn);
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
if (isStoredProcedure)
cmd.CommandType = CommandType.StoredProcedure;

rowsAffected = cmd.ExecuteNonQuery();
}
return rowsAffected;
}

public class ParamBuilder
{
private readonly List<SqlParameter> _parameters = new List<SqlParameter>();
public List<SqlParameter> Parameters
{
get
{
return _parameters;
}
}

public void AddParam(SqlDbType sqlDbType, string paramName, object paramVal)
{
SqlParameter p = new SqlParameter(paramName, sqlDbType);
p.Value = paramVal ?? DBNull.Value;
_parameters.Add(p);
}

public SqlParameter AddOutputParam(SqlDbType sqlDbType, string paramName)
{
SqlParameter p = new SqlParameter(paramName, sqlDbType);
p.Direction = ParameterDirection.Output;
_parameters.Add(p);
return p;
}
}


}
}


Example 2) Sample Usage of ExecuteReader()



Note you should wrap the ExecuteReader() in a using statement.



public static Address GetAddressByID(int addressID)
{
Address address = null;

DataGeneral.ParamBuilder paramBuilder = new DataGeneral.ParamBuilder();
paramBuilder.AddParam(SqlDbType.Int, "@AddressID", addressID);

using (SqlDataReader sqlDataReader = DataGeneral.ExecuteReader("Location_Address_GetByID", paramBuilder.Parameters, true))
{
if (sqlDataReader.Read())
{
address = PopulateAddressFromReader(sqlDataReader);
}
}

return address;
}

private static Address PopulateAddressFromReader(SqlDataReader sqlDataReader)
{
Address address = new Address();

address.AddressID = (int)sqlDataReader["AddressID"];
address.AddressLine1 = sqlDataReader["AddressLine1"] as string;
address.AddressLine2 = sqlDataReader["AddressLine2"] as string;
address.AddressLine3 = sqlDataReader["AddressLine3"] as string;
address.City = sqlDataReader["City"] as string;
address.Zipcode = sqlDataReader["ZipCode"] as string;
address.CountryRefID = (int)sqlDataReader["CountryRefID"];
address.CityLatitude = sqlDataReader["CityLatitude"] as float?;
address.CityLongitude = sqlDataReader["CityLongitude"] as float?;
address.ZipcodeLatitude = sqlDataReader["ZipcodeLatitude"] as float?;
address.ZipcodeLongitude = sqlDataReader["ZipcodeLongitude"] as float?;

return address;
}


Example 3) Sample usage of ExecuteNonQuery()



Note, that both inline sql and stored procedures are supported. This example also demonstrates output parameters. For example, if you are inserting in to a table with an identity column and want to return the newly created identity value.



This example also demonstrates wrapping a call with TransactionScope() which is another great feature of the .NET framework.



public static void AddAddress(Address address)
{
DataGeneral.ParamBuilder paramBuilder = new DataGeneral.ParamBuilder();
SqlParameter paramNewAddressId = paramBuilder.AddOutputParam(SqlDbType.Int, "@NewAddressID");
paramBuilder.AddParam(SqlDbType.VarChar, "@AddressLine1", address.AddressLine1);
paramBuilder.AddParam(SqlDbType.VarChar, "@AddressLine2", address.AddressLine2);
paramBuilder.AddParam(SqlDbType.VarChar, "@AddressLine3", address.AddressLine3);
paramBuilder.AddParam(SqlDbType.VarChar, "@City", address.City);
paramBuilder.AddParam(SqlDbType.VarChar, "@StateProvinceRefID", address.StateProvinceRefID);
paramBuilder.AddParam(SqlDbType.VarChar, "@ZipCode", address.Zipcode);
paramBuilder.AddParam(SqlDbType.VarChar, "@CountryRefID", address.CountryRefID);
paramBuilder.AddParam(SqlDbType.Float, "@CityLatitude", address.CityLatitude);
paramBuilder.AddParam(SqlDbType.Float, "@CityLongitude", address.CityLongitude);
paramBuilder.AddParam(SqlDbType.Float, "@ZipcodeLatitude", address.ZipcodeLatitude);
paramBuilder.AddParam(SqlDbType.Float, "@ZipcodeLongitude", address.ZipcodeLongitude);

const string sql =
@" INSERT Address
( AddressLine1, AddressLine2, AddressLine3, City, StateProvinceRefID, ZipCode, CountryRefID,
CityLatitude, CityLongitude, ZipcodeLatitude, ZipcodeLongitude, AddressPhoneID,AddressFaxPhoneID,
CreatedDate, CreatedBy, UpdatedDate, UpdatedBy)

VALUES
(@AddressLine1,@AddressLine2,@AddressLine3,@City,@StateProvinceRefID,@ZipCode,@CountryRefID,
@CityLatitude,@CityLongitude,@ZipcodeLatitude,@ZipcodeLongitude)

SET @NewAddressID=SCOPE_IDENTITY();";

using (TransactionScope transactionScope = new TransactionScope())
{
DataGeneral.ExecuteNonQuery(sql, paramBuilder.Parameters, false);
transactionScope.Complete();
}
address.AddressID = (int) paramNewAddressId.Value;
}

Comments

Popular posts from this blog

How to Create and Run Tableau Bridge on Linux Containers

Tableau Bridge is now availble on Linux Containers. Yay! Now what does this mean and how do I build and run Linux Containers? We will discuss the advantages of running Bridge on Linux Containers the steps to build them, and finally, we will provide some automation script ideas for monitoring and scaling Linux Bridge agents. Tableau Bridge Today Until recently, Tableau Bridge was only available as a Windows application running on a Windows VM. It supported only one bridge agent per Virtual or Physical Machine. Advantages of Bridge in Containers Better Hardware Utilization: Linux containers are more efficient than Windows VMs, requiring only about 1/50th of the disk space. Ability to Spin Up Multiple Bridge Agents: With Linux Containers, it becomes easier to spin up multiple bridge agents on a single machine, improving scalability and resource utilization. Infrastructure Automation: Linux Containers enable easier automation of provisioning bridge agents and upgrading Tableau Bridge, the...

Unleashing Tableau’s Semantic Layer with AI Agents

⚡ TL;DR I helped built a tool that lets you query Tableau’s semantic layer  using natural language and AI. By integrating a LangChain agent with Tableau’s VizQL Data Service (VDS), we can repurpose Tableau’s trusted data model for conversational analytics . This means you can ask questions in plain English and get answers backed by the same definitions and security that your Tableau dashboards use. In this post, I’ll introduce this open-source agentic tool ( tableau_langchain ), why it’s transformative for analytics, and how it works under the hood. Why Connect LangChain Agents to Tableau? As a user of Tableau, I’ve seen how powerful Tableau’s semantic layer is. It encapsulates our organization’s business logic: things like predefined metrics, calculations, data relationships, and even row-level security rules. Traditionally, that semantic layer is only accessible through Tableau’s interface – you drag and drop fields to build a viz, and Tableau generates the query for you. Rece...

RAM Disks do not speed up Visual Studio

  The limiting factor for Visual Studio is disk IO. I got a tip to speed up Visual Studio from Channel 9 by creating a RAM disk which sounded like a great idea. However, when I ran a thorough set of tests, I found that the performance difference between the Ram disk and the hard disk were not appreciably different. This was a big surprise since RAM is 240,000 times faster than disk (see my previous blog post). But the reason is because Visual Studio and Vista do a lot of caching. So compile times for the same project in RAM disk and on hard disk were pretty similar. I also tested the time it took to search the entire solution for a word, and times to open a solution. There was no discernable difference!   If you still want to try it out and create your own RAM disk, you can download a simple RAMDISK.EXE utility to create a RAM disk in just a few minutes. What is a RAM Disk ?   Ramdisk is a virtual drive created in RAM.   Performance Analysis Creating f...