目标: 使用C#和SQL2008正确设置参数化的SQL插入语句
问题: for循环中使用了以下语句,因此必须清除这些值。运行此代码后,它指出在250附近有语法错误。代码如下
for (int i = 0; i < Rows.Count; i++) { cmd.Parameters.Clear(); struct Row = (struct)Rows[i]; sql = "@RowName varchar(250) = null " + "INSERT INTO " + "database.dbo.table" + "(database.dbo.tabe.RowName) " + "VALUES " + "(@RowName) "; cmd.CommandText = sql; cmd.Parameters.AddWithValue("@RowValue ", Row.RowName);
}
在此先感谢您的更正,评论和建议。
您不必在SQL代码中重新声明变量。这应该工作:
sql = "INSERT INTO " + "database.dbo.table" + "(database.dbo.tabe.RowName) " + "VALUES " + "(@RowName) "; cmd.CommandText = sql; cmd.Parameters.AddWithValue("@RowValue ", Row.RowName);