Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
for (int j = 1; j < lineData.Length; j++)
                {
                    
                    query2.Append("insert into ");
                    query2.Append(tableName);
                    query2.Append(" values");
                    query2.Append("(");
                    string[] Row = lineData[j].Split('\t');


                    for (int k = 0; k < ((Row.Length) - 1); k++)
                    {
                        query2.Append("@");
                        query2.AppendFormat("[{0}]",fields[k]);
                     }
                    query2.Append(");");
                 SqlCommand command2 = new SqlCommand(query2.ToString(),myConnection);
                    
                    for (int k=0; k < (Row.Length - 1); k++)
                    {
                        StringBuilder parm = new StringBuilder();

                        parm.Append("\"");
                        parm.Append("@[");
                        parm.Append(fields[k]);
                        parm.Append("]");
                        parm.Append("\"");
                        Console.WriteLine(parm.ToString()+" "+Row[k]);
                        command2.Parameters.AddWithValue(parm.ToString(), Row[k]);
                        
                    } command2.ExecuteNonQuery();

                }
Posted
Updated 15-Dec-14 20:16pm
v2
Comments
Kornfeld Eliyahu Peter 15-Dec-14 16:33pm    
It seems that the final SQL query has a syntax error - most likely a missing variable name...However it is impossible to say as we can't see that query...Debug You code and check what the actual command is...
ZurdoDev 15-Dec-14 16:40pm    
This would be very easy to fix if you debug it. You have a syntax error.
PIEBALDconsult 15-Dec-14 17:11pm    
What comes to mind immediately is to ensure any null parameter values are set to DBNull.Value
Mycroft Holmes 16-Dec-14 0:42am    
Want to test the new spam filter.

Ok so the spam filter will let me make a comment but not post a reply.
_Maxxx_ 16-Dec-14 0:53am    
As others have said - debug it and look at the sql being performed

But the fact that the error says incorrect syntax bear nvarchar rather imp-lies there is an "nvarchar" in the sql string being built, which doesn't seem logical as you are building an insert statement - so maybe your source data doesn't look like what you think?

1 solution

The @ is only used when you are using the SqlParameter, when you are building string queries you should not be using @[colname] but should be using [colname].

When using the SqlParameter you don't need to use square brackets [] -> @varname will do.

Remove the @ prefix from your string queries.
 
Share this answer
 
v2

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

  Print Answers RSS


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