mostlylucid

scott galloway's personal blog...
posts - 916, comments - 758, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Simple pattern for the Data Access Application block

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.

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

Comments have been closed on this topic.

Powered by: