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.
© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.