c# - Parameterized queries on iteration -
I am including web form in database and therefore using parameterized queries. I have a checkboxist. How can I repeat more than CheckBoxList, to create a insert statement for every check (multiple-to-many), and to keep this parameditized and killed at one time
< P> I enter this now: string query = "resources (URLs, submitted_by, AUTHOR_NAME) values (@url, @submitted_by, @author_name);"; Foreign Currency (Listed in the checkbox list 1). If (li.Selected = true) {query + = "INSERT IN"; }} SqlCommand CMD = New SqlCommand (query, conn); Cmd.Parameters.AddWithValue ("@ url", TextBox1.Text); Cmd.Parameters.AddWithValue ("@ submitted_by", TextBox2.Text); Cmd.Parameters.AddWithValue ("@ author_name", TextBox3.Text); Try {conn.Open (); Cmd.ExecuteNonQuery (); Label1.Text = "Added to database."; }
As you can see, it is incomplete any suggestions?
You can use LINQ to generate unique name parameters for each item in your archive , Then you can add the associated values to later versions:
var builder = new StringBuilder (); The above listParams = CheckBoxList1.Items .Where (li = & gt; li.Selected) .Select (Lee, IDX => New {PhoneString = String.Format ("@ phone_id {0}", IDX), PhoneValue = GetPhoneId (Lea), ResourceString = String.Format ("@ource_id {0}", IDX), Resource Value = GetResourceId (li)}; foreach (various paramas in list paramours) {builder.AppendFormat ("Phone_sets (Phone_ID, in resource_id) Include values ({0}, {1}); "param.PhoneString, param.ResourceString";} SqlCommand cmd = new SqlCommand (builder.ToString (), cone); foreach (listParams on top) {cmd .Parameters.AddWithValue (param.PhoneString, param.PhoneValue); cmd.Parameters.AddWithValue (param.ResourceString, param.ResourceValue);}
I will add you I'm thinking that there is a way to be there, code_id, RESOURCE_ID from any Note: Switching to a ListItem
- you can simply plug me where I go to placeholder Code> tasks.
stringbuilder
- is better than creating a string with repeated + =
< / P>
Comments
Post a Comment