SqlCommand.Parameters.Add() vs SqlCommand.CreateParameter()

When doing some quick research on SQL Parameters with SqlCommand, I noticed two methods which seemed to perform the same task, SqlCommand.Parameters.Add() and SqlCommand.CreateParameter().  After looking into each method on MSDN, I noticed that CreateParameter() will simply create the parameter, while Parameters.Add() will create the parameter and add it to the parameter collection.


The following example will create a SQL command object, create and add to the parameter list in one step, then add and assign values to the parameters.

SqlCommand command = new SqlCommand(someCommandText,someConnection)
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].Value = customerID;


The following will create a sql command object, add and assign values, then add to the parameter list. A few more steps than the previously mentioned Parameters.Add()

SqlCommand command = new SqlCommand(someCommandText,someConnection)
SqlParameter sqlParam = command.CreateParameter();
sqlParam.SqlType = SqlType.Int;
sqlParam.Value = 5;
sqlParam.ParameterName = "ID";

Releated Post

Leave a Reply

%d bloggers like this: