0

Problem statement.

Basically I get 3 - 50 parameters that come back from a web service as a NVP array I then need to loop over them create the SQL command parameters for each and call the stored procedure. Is there a more efficient way to handle it than the approach below?

 using (SqlConnection connection = new SqlConnection(connectionString))
                         {
                             connection.Open();
                             using (SqlCommand cm = connection.CreateCommand())
                             {
                                 cm.CommandText = "MySproc";
                                 cm.CommandType = CommandType.StoredProcedure;
                                 foreach (var field in row)
                                 {

                                     cm.Parameters.AddWithValue("@" + field.Key.ToString(), field.Value.ToString());
                                 }
                                 cm.ExecuteNonQuery();
                             }
                         }
GoBeavs
  • 499
  • 2
  • 10
  • 25
  • Is the stored procedure set up to handle NULL parameters (i.e., `@Param1 AS INT = NULL, @Param2 AS BIT = NULL`, etc)? – Tim Sep 26 '12 at 04:00

1 Answers1

0

I personally use the ISNULL or COALESCE in the WHERE clause of the stored procedure. Unless your looking to do it inside your c#...

http://blogs.x2line.com/al/archive/2004/03/01/189.aspx

hagensoft
  • 1,497
  • 13
  • 13