c# - SQL ExecuteNonQuery throws "Incorrect syntax near '0'." -


I have the following code that I am using to insert several rows in the database:

< Pre> _conn.Open (); SqlCommand command = new SQL Commands (_insert [0]. Ostring (), _conn); order. Parameter. Add-Range ((_ [Insert [1] list as & lt; SqlParameter & gt;) ToArray ()); Int 32 Rows Affected = Command.exequnet (); _conn.Close ();

The command (_Instant [1] .tostring ()) is:

  "Include the candidate (id, name) values ​​({0} , {1}), ({2}, {3}), ({4}, {5}), ({6}, {7}), ({8}, {9}), ({10} , {11}), ({12}, {13}), ({14}, {15}), ({16}, {17}), ({18}, {19}), ({20} , {21}), ({22}, {23}), ({24}, {25}), ({26}, {27}), ({28}, {29}), ({30 }, {31}), {{{{}}, {33} >  

and the command parameters are like this (from VS debug):

  [0] - parameter name = "0", SqlDbType = BigInt, value = 21400 [1 ] - parameter name = "1", SqlDbType = NvarChar, Value = "Dan Smith" [2] - parameter name = "2", SqlDbType = BigInt, Value = 21401 [3] - parameter name = "3", SqlDbType = NvarChar,= "Doug Smith" [4] - parameter name = "4", SqlDbType = BigInt, value = 21402 [5] - parameter name = "5", SqlDbType = NvarChar, value = "Daniel Smith"  

The error I am getting is '0' with the wrong syntax. Can anyone tell me what is the reason?

If this is the case here, where I am creating parameter array (different function which replicates JSON, returning from the API): < / P>

  Foreach (keyValuePair & lt; string, object & gt; _attribute in _field) {// '_score' is a feature included with each unit, when a search matches the // Indicates, we do not want it in the database (_attribute.Key.ToString ()! = "_score") {_insertQuery.AppendFormat ("{{{}}},", _paramNumber); _paramValues.Add Or SqlParameter (_paramNumber.ToString (), _attribute.Value)); _paramNumber ++;}} Return the new object [] {_inertQuery.ToString (), _paramValues};  

I Because I am retrieving various objects from an API and then joining a local database. I can not create a class for each of these objects (requirements are to make this normal).

UPDATE :

I with _paramNumber "@", so each parameter @ 0, @ 1 etc. And now I am getting this error:

  "Wrong syntax near '@ 0'."  

The included statement is as follows:

  "candidates (id, name) }), ({@ 6}, {@ 7}), ({@ 8}, {@ 9}), ({@ 10}, {@ 11}), ({@ 12}, {@ 13}) , ({@ 14}, {@ 15}), ({@ 16}, {@ 17}), ({@ 18}, {@ 19}), ({@ 20}, {@ 21}) {@ 22}, {@ 23}), ({@ 24}, {@ 25}), ({@ 26} {@ 27}), ({@ 28}, {@ 29}), ({@ 30 }, {@ 31}), {{32}, {@ 33}), ({@ 34}, {@ 35}), ({@ 36}, {@ 37}), ({@ 38} {@ 39}) "and  

and the parameters are as follows:

  [0] - parameter name =" @ 0 ", SqlDbType = BigInt, Value = 21400 [1] - Param Paramount Name = "@ 2", SqlDbType = BigInt, Value = 21401 [3] - Parameter Name = "@ 3", SqlDbType = NvarChar, Surname = "@ 1", SqlDbType = NvarChar, Value = "Dan Smith" Value = "Doug Smith" [4] - Parameter name = "@ 4", SqlDbType = BigInt, Value = 21402 [5] - Parameter name = "@ 5", SqlDbType = NvarChar, Value = "Daniel Smith"  < / Pre> 

If you are using direct parameters in your query and are not formatting, Along with the formatting, you can only query parameters in your query And should use, such as.

Insert in the candidate (id, name) values ​​(@ 0, @ 1), (@ 2, @ 3), (@ 4, @ 5), (@ 6, @ 7), (@ 8, @ 9), (@ 10, @ 11), (@ 12, @ 13), (@ 14, @ 15), (@ 16, @ 17), (@ 18, @ 19 ), @ @, @ 21), (@ 22, @ 23), (@ 24, @ 25), (@ @ 26) 9) "

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -