c# - SqlCommand AddWithValue not working properly -


I have the following block of code ... fieldCount always returns 0.

I suspect that AddWithValue is not properly writing SELECT statement ... no idea why? As you can see from the watch, they are valid values ​​( field , fieldId ).

  Public Zero deleteRows (string table, string field, string field id) {int fieldCount; (SqlCommand command = new SqlCommand (String.Format ("SELECT COUNT (*) FROM {0} WHERE @field IN (@fieldId)",), {command.Parameters.AddWithValue ("@ field", using ) Farm); order. Parameter. Edvat Value ("@field ID", Field ID); FieldCount = (int) command. Exclute form (); } If (field count> 0) {  

debug screen

There are two errors in your code:

First of all, you do not use the parameter Can you name the name of a table or column so your field parameter is not valid in this context.

Secondly, you can not use a parameter to express the full values ​​of these segments. Your example will translate the parameter @ field ID

  WHERE ..... IN ('1,2,3,4')   

Will be treated as a string, as different values ​​are not included in your reference -

For the field part, If you are absolutely sure that the string field The parameter passed to your method is not directly typed by your user, so you have a string containment expression You can use the person (well you're already doing this for table so that the warning is also good for that value)

  String.Format ("SELECT COUNT (*) FROM {0} WHERE {1} IN (....) ", table, area);   

For IN part, I suggest, in the calling function, instead of passing the string to create a list of parameters to be added to the query.

  Public Zero removal (string table, string field, list & lt; SqlParameter & gt; inParameters) {StringBuilder sb New StringBuilder (); Where {1} IN (", table, field) from Sb.AppendFormat (" SELECT COUNT (*)); (Using SQL Commands CMD = New SQL Commands ()) (CLD Connection = connection; // Loop on the list of parameters, add the parameter name to these sections and parameter for SqlCommand archive foreach (SqlParameter p in inParameters } {sb.Append (p.Name + ","); cmd.Parameters.Add (P);} // Final Comma SB Length ---- Trim Out;; // Close this section; snApp.Append (")"; Cmd.CommandText = sb.ToString (); FieldCount = (int) command.Excllicate Ascendant ();}}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -