Back to "Simple pattern for the Data Access Application block"

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

Imported mostlylucidcouk

Simple pattern for the Data Access Application block

Friday, 08 September 2006

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. 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.

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.