I have the following SQL statement that I want to run:
string sql = @"DECLARE @a udt;" +
"INSERT INTO @a (id) VALUES @params;";
I have the following array of integers:
int[] array1 = {10,20,30,40,50,60};
It is important to not that the above array size is dynamic in my program.
I would like to add the values from the array to the @params in the SQL statement so that the SQL being executed in the SqlCommand looks like this:
sql = @"DECLARE @a udt;" +
"INSERT INTO @a (id) VALUES (10),(20),(30),(40),(50),(60);" +
"EXEC sp @a;";
I have tried the following amongst others and keep getting exceptions:
SqlConnection con = new SqlConnection("connectionString");
SqlCommand cmnd = new SqlCommand(sql, con);
for (int i = 0; i < array1.Count; i++)
{
cmnd.Parameters.AddWithValue("@params" , array1[i]);
}
con.open().
cmnd.ExecuteNonQuery();
May somebody please explain what I am doing wrong. I also need to make sure to prevent SQL injection.
...VALUES @param1, @param2...instead of...VALUES (10), (20)...?DECLARE @a udt;there?