Adding multiple parameters all at once to a SqlCommand using AddRange

28 Feb

So rarely does a Stored Procedure take just one argument and well typing the same thing over and over and over can be a pain so what to do? use AddRange and create a array of sql command using simple syntax and copy pasta

 using (SqlCommand cmd = new SqlCommand() { CommandType = CommandType.StoredProcedure, Connection = connString, CommandText = "Exec Card_Add_Company" })
cmd.Parameters.AddRange(new SqlParameter[]
new SqlParameter("@Example",NewExampleInfo.Rows[0]["CompanyName"].ToString()),
new SqlParameter("@Example_Contact",NewExampleInfo.Rows[0]["Name"].ToString()),
new SqlParameter("",[ControlNameHere]),
new SqlParameter("",[ControlNameHere])

Using this then filling in the blanks can save you quite a bit of time and you get those 13-20 something variables out of the way in no time for me it was only nine so i made 9 SqlParameters in the add range statement and bam love is in the air. Make a custom snippet for this to use with intelisense and you are golden.

Leave a comment

Posted by on February 28, 2012 in C#, SQL


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: