The error message is as follows:
Unhandled exception of type "System.Data.SqlClient.SqlException" occurs in System.Data.dll
Additional information: "" There is a syntax error nearby.
The text version of the code is as follows:
1 sqlcmd.commandtext =string"insert INTO [dbo].[ Table4test] ([Id],[name],[password]) values ({0}, ' {1} ', ' {2} ')", dataofitem[0],dataofitem[ 1],dataofitem[2]); 2 3 Sqlcmd.executenonquery ();
It can be seen that the error is the sentence of the SQL statement.
Since the question of SQL statements why not put it into SQL to debug it?
So and I extracted the error from the program error when the SQL statement.
CTRL + C, CTRL + V , I copied it to SQL Server Management Studio.
F5 run.
After some modification, the confirmation is the parentheses problem, after re-entering the parentheses, the data is successfully put into storage.
It seems to be just the half-width and full-width of the brackets, but I was wrong.
CTRL + C, CTRL + V , I copied it to Microsoft Visual Studio.
Continue to error.
Then I went through the following attempts, none of the results.
1. Replace the String.Format () method with the plus operator overload of string.
2. Change from visual Studio 2012 to 2015.
3. Replace the. Net framework from 4.6.1 to 4.5.
4. Restart the instance of SQL Server.
5. Restart the computer.
The only successful one was when I manually punched the SQL statement, like this:
1 sqlcmd.commandtext ="insert INTO [dbo].[ Table4test] ([Id],[name],[password]) VALUES (0, ' Jingyu ', ' 1 ')"; 2 3 sqlcmd.executenonquery ();
The problem appears to be on a string merge.
Based on RUNLIUV's recommendation, I exported the text of the SQL statement using the File.appendalltext () method and set the notepad++ software to display all symbols.
The exception is not visible.
Finally put the environment:
System: Windows 10 Professional Edition 1511.
Ide:microsoft Visual Studio 2012/2015.
. NET Framework:. NET framework4.5/4.6.1
SQL Server:microsoft SQL Sever 2012.
By the way, there are the same cases.
http://bbs.csdn.net/topics/390687746
A bug was encountered when inserting data to SQL Server in C #