This article summarizes some nice ways to create SqlParameter arrays. When we use SqlHelper, or even without SqlHelper when we use SqlCommands directly, and we want to pass an array of SqlParameters to the function. The method I will discuss does not require you to hard-code the number of elements, nor does it require you to create a temporary list of some sort, but it requires you to have all the values up front that you want to insert into the list.
There are several ways to create this array.
- One is to hardcode the number of parameters as follows:
SqlParameter array SqlParameter[] sqlParams = new SqlParameter[5];
- An evolution of this is to use an ArrayList
ArrayList sqlParams = new ArrayList();
Then add the parameters using sqlParams.Add(…) to add them
Then the final step is to use sqlParams.ToArray() and pass that to your SqlHelper. - Another evolution would be to use a Generic list of SqlParameters
System.Collections.Generic.List<SqlParameter> sqlParams = new System.Collections.Generic.List<SqlParameter>(); - But in my opinion the BEST way of creating your SqlParameter array is as follows:
C# Example:
Vb.Net Example:
You can even use conditionals such as if your questionCode is null you want to insert a null (C# example):
4.
SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter("@username", strUserName) , new SqlParameter("@password", strPassword) }; Dim params As SqlParameter() = { _ New SqlParameter("@CenterID", centerID), _ New SqlParameter("@TypeID", typeID) _ } SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter("@Required", required), questionCode == null ? new SqlParameter("@Code", DBNull.Value) : new SqlParameter("@Code", questionCode) };
Update Aug7’09 – Not specifying the DB type of the parameter will actually be slower than manually specifying the parameter sql types (ie int, varchar, etc). However, I find the tradeoff for convenience to be worth a slight loss of speed. This also allows you to change the parameter type in the stored procedure (say from datetime to string), without having to also update it here.
Update: Here is some example code using SqlParameters and DataAdapters



Sameer, thank you for the tip of using a List and then using the ToArray() method. Have a nice day.
What a frick’n joke – if you’re passing null, why the hell doesn’t the underlying code just put DBNull.value for you instead of making every single developer have to do this. Madness.
i hav no comment but i need ur help ie. i want to insert data to sql database using Array using sqlparameter so can u help..syntx is like
Dim SqlParameter[] P= New SqlParameter[5]
P[0]=New SqlParameter["@Id",Id]
this is error so hu can u help me..please help me
[...] you read Creating SqlParameters Best Practices you will find the fun you have if you have null [...]
Settings DBNull.Value to parameter without specifying DbType leads to conversion errors in some cases, e.g. VarBinary.
Its also slightly slower, however the time savings for me is worth not specifying the type. Plus I hate changing the code everytime the stored proc changes parameter types.
The formatting is totally off on Internet Explorer 8…
I fixed it.. sorry bout that.
Nice, this looks way better, Sameer. Thanks for fixing it.
Formatting of this page is still scrambled in IE8.