A bug was encountered when inserting data to SQL Server in C #

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.