Click here to Skip to main content
16,023,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,


Is it possible to insert multiple values through single statement
That is to assign the values to the parameter through single "add new Sqlparametr" statement


I wrote code in separate statements to add the parameter values

I wrote code as follows

String str = "insert into State(Code,Name)values (@Statecode1,@Statename1)";
cmd.Parameters.Add(new SqlParameter("Statecode1", this.txtstatecode.Text));
cmd.Parameters.Add (new SqlParameter ("Statename1", this.txtstatename.Text));
cmd.CommandText = str;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
Posted

1 solution

First of all, this has nothing to do with WPF. Nothing at all. Moving on, you can add multiple parameters with a single statement, if you create a function to do that for you:
C#
public void AddParameters(SqlCommand cmd, params SqlParameter[] parameters)
{
	foreach (SqlParameter p in parameters)
	{
		cmd.Parameters.Add(p);
	}
}

You can call that like this:
C#
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable WHERE C1=@P1 AND C2=@P2");
AddParameters(cmd, new SqlParameter("P1", "..."), new SqlParameter("P2", "..."));

By using the params keyword, you can pass as many SQL parameters as you want to the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900