Guest User

Guest User

  • Tech Writer
  • 357
  • 120.9k

Understanding what a particular method is doing?

Mar 1 2012 10:48 AM
Hi, I am trying to make sense of the below method, I would appreciate any correction\advice against my comments please

// Method signature declaration which accepts 2 parameters, the first a Database Command and the second an array of parameters of type Object       

public static void SetParameters(this DbCommand command, object[] parms)

// Validation check to see the array of parameters is not null and of a length greater than 0

            if (parms != null && parms.Length > 0)

// Loop through and add 2 to each loop???

                for (int i = 0; i < parms.Length; i += 2)

// Convert the first parameter to string and store in a name variable

                    string name = parms[i].ToString();

// Not entirely sure

                    if (parms[i + 1] is string && (string)parms[i + 1] == "")

// Reset to null

                        parms[i + 1] = null;

// Again not entirely sure

                    object value = parms[i + 1] ?? DBNull.Value;

//  Create the parameter object and and store in a local var named dbParameter

                    var dbParameter = command.CreateParameter();

// Set the name reference to the ParameterName property

                    dbParameter.ParameterName = name;

// Set the value reference  to the Value property

                    dbParameter.Value = value;

// The add the dbParameter to the list of command parameters

                    command.Parameters.Add(dbParameter);


Thanks


Answers (4)