# Simple pattern for the Data Access Application block

<datetime class="hidden">2006-09-08T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported -->

## 
Simple pattern for the Data Access Application block 

posted on Friday, August 27, 2004 1:38 PM

Thought I'd just throw out a simple code sample for the (very) simple pattern I use when writing data access code with the [Data Access Application Block](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp). This example is for doing a simple object insert (non-transactional) into a DB:

internal static  void AddEmailAddressToContact(Int64 contactId, EmailAddress emAddr)

{

string spName = "pr\_MedSh\_AddEmailAddressToContact";

SqlParameter[] sqlParams = SqlHelperParameterCache.GetSpParameterSet(Global.Config.DBConnectionString,spName);

sqlParams[0].Value = contactId;

sqlParams[1].Value = emAddr.AddressTypeId;

sqlParams[2].Value = emAddr.IsPrimary;

sqlParams[3].Value = emAddr.Address;

SqlHelper.ExecuteNonQuery(Global.Config.DBConnectionString,CommandType.StoredProcedure,spName,sqlParams);

}

Told you it was simple! This is the pattern I use for all these sort of things. Using a SqlDataReader is just as easy, I wrap the 'SqlDataReader dr = SqlHelper.ExecuteReader' in a 'using' block and very rearely pass it up between layers, preferring instead to use simple custom collections or ArrayLists.