Technie
Technical information, links, and my own programming ideas.

David-ism
Watu
Vicariously
Photo Blogs
Form, Function
Write, Think
Web People
Coders
Feel Good


Subscribe to "Technie" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Home (all entries)  | Technie  | Prattle (personal stuff)  | Books  | Snippets  | WhiteBox


Wednesday, October 23, 2002
 

Executing Stored Procs with ADO.NET

Days like these.  Today I spent most of the day ironing out one of about 6 stored procedures related to underwriting for our loan application.  We still have yet to put all the pieces together but sigh, there's no end in sight right now.

I feel quite proud of myself though; the way our code is formated at this point it's really easy to track down bugs like missing parameters or invalid data types.

I always follow the same approach to putting together stored procedures:

1.  Declare an ArrayList for the parameters.
2.  Add the params as anonymous instances of SqlParameter using the 2 part constructor (paramname, value).
3.  Create command, iterate the ArrayList and add to the parameters collection.

Some sample code:

 

// build parameter definitions
ArrayList parms = new ArrayList();
// parameters that arrive from the arguments
parms.Add(new SqlParameter("@CompanyID",m_companyid));
parms.Add(new SqlParameter("@CustNum",m_cust));
parms.Add(new SqlParameter("@Info",story.Information));
// ... and so on

// trap output parameter SqlParameter ident = new SqlParameter("@TTStoryID",SqlDbType.Int); ident.Direction = ParameterDirection.Output; parms.Add(ident); // build command... SqlCommand insStory = new SqlCommand("uspTeletrackStoryInsert"); insStory.CommandType = CommandType.StoredProcedure; // append parameters foreach(object obj in parms){ insStory.Parameters.Add((SqlParameter)obj); } insStory.Connection = myconnection; insStory.ExecuteNonQuery();

QED, really.


7:17:35 PM    comment []


Click here to visit the Radio UserLand website. © Copyright 2006 David Seruyange.
Last update: 5/23/2006; 8:46:19 PM.
October 2002
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Sep   Nov