不正确的语法附近'S'。字符串后未闭合的引号')' [英] incorrect syntax near 's'. unclosed quotation mark after the character string ')'
问题描述
我有点儿编程小白,我想知道我做了什么错在这里 - ?可有一个人帮我。
我正在在一个控制台应用程序它同步两个数据库,但是当我尝试将数据插入到表中它抛出该异常;该代码是:
公共静态无效AddInterationPath(SqlConnection的conDS_ReleaseCri,DataRow的DR)
{
的SqlCommand insertNewAreaPath =新的SqlCommand(INSERT INTO InterationPath(ID,的NodePath)VALUES(+ DR [0]的ToString()+,'+ DR [2]的ToString()+'),conDS_ReleaseCriterions);
insertNewAreaPath.ExecuteNonQuery();
}
公共静态无效AddAreaPath(SqlConnection的conDS_ReleaseCri,DataRow的DR)
{
的SqlCommand insertNewAreaPath =新的SqlCommand(INSERT INTO AreaPath(ID,的NodePath)VALUES( + DR [0]的ToString()+,+ DR [2]的ToString()+'),conDS_ReleaseCriterions);
insertNewAreaPath.ExecuteNonQuery();
}
和我得到了以下异常:
不正确附近有语法'S'。字符
字符串后未闭合的引号')
块引用>
解决方案数据你很可能在插入包含特殊字符,如单引号。更改为一个参数化查询以便值正确转义。一个很好的例子,并解释是的http:// www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html 。
例如,以取代你的第一个函数的内容C>的SqlCommand insertNewAreaPath =新的SqlCommand(
INSERT INTO InterationPath(ID,的NodePath)VALUES(@ID,@NodePath),
conDS_ReleaseCriterions);
insertNewAreaPath.Parameters.Add(@ ID中,博士[0]);
insertNewAreaPath.Parameters.Add(@的NodePath,博士[2]);
insertNewAreaPath.ExecuteNonQuery();
I am kinda noob in programming, and I was wondering what I have done wrong here - can some one help me out?
I am making a console app in which it syncs two databases, but when I try to insert the data into the table it throws this exception; The code is:
public static void AddInterationPath(SqlConnection conDS_ReleaseCri, DataRow dr) { SqlCommand insertNewAreaPath = new SqlCommand("INSERT INTO InterationPath (ID,NodePath) VALUES(" + dr[0].ToString() + ",'" + dr[2].ToString() + "')", conDS_ReleaseCriterions); insertNewAreaPath.ExecuteNonQuery(); } public static void AddAreaPath(SqlConnection conDS_ReleaseCri, DataRow dr) { SqlCommand insertNewAreaPath = new SqlCommand("INSERT INTO AreaPath (ID,NodePath) VALUES(" + dr[0].ToString() + ",'" + dr[2].ToString() + "')", conDS_ReleaseCriterions); insertNewAreaPath.ExecuteNonQuery(); }
And I get the following exception:
incorrect syntax near 's'. unclosed quotation mark after the character string ')'
解决方案The data you are inserting probably contains special characters like single quotes. Change to a parameterized query so the values are escaped properly. A good example and explanation is http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html.
[Edit: Added an example. ]
For example, replace the contents of your first function with:
SqlCommand insertNewAreaPath = new SqlCommand( "INSERT INTO InterationPath (ID, NodePath) VALUES(@ID, @NodePath)", conDS_ReleaseCriterions); insertNewAreaPath.Parameters.Add("@ID", dr[0]); insertNewAreaPath.Parameters.Add("@NodePath", dr[2]); insertNewAreaPath.ExecuteNonQuery();
这篇关于不正确的语法附近'S'。字符串后未闭合的引号')'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!