我在编程方面有点菜鸟,我想知道我在这里做错了什么 - 有人可以帮助我吗?
我正在制作一个控制台应用程序,在其中同步两个数据库,但是当我尝试将数据插入表中时,它会抛出此异常;代码是:
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();
}
我得到以下异常:
's' 附近的语法不正确。字符后未闭合的引号
细绳 ')'
您插入的数据可能包含特殊字符,例如单引号。更改为参数化查询,以便正确转义值。一个很好的例子和解释是http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html.
[编辑:添加了一个示例。 ]
例如,将第一个函数的内容替换为:
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)