C#SqlCommand.ExecuteNonQuery为成功更新SQL语句返回-1
使用C#SqlCommand.ExecuteNonQuery为成功更新SQL语句返回-1,c#,sql-server,C#,Sql Server,使用.ExecuteNonQuery,以下代码似乎正常工作: public static void StringQueryWithParameterList( string queryString, List<string> keys, List<string> values ) { SqlCommand command = new SqlCommand(queryString); int affected; comman
.ExecuteNonQuery
,以下代码似乎正常工作:
public static void StringQueryWithParameterList(
string queryString,
List<string> keys,
List<string> values
)
{
SqlCommand command = new SqlCommand(queryString);
int affected;
command.Connection = GetConnection();
command.CommandType = CommandType.Text;
for (int i = 0; i < keys.Count; i++) {
command.Parameters.AddWithValue(keys[i], values[i]);
}
affected = command.ExecuteNonQuery(); // Note: this is returning -1 ... why?
if (affected < 1)
{
throw new Exception("No rows were updated by this update query.");
}
}
在实例级别,
SET NOCOUNT
可能是开启的
要检查它,可以使用dbccuseroptions代码>
要想关闭NOCOUNT
,您可以
sp_configure 'user options' 0
PS:AddWithValue(键[i],值[i])
不是类型安全的。我不建议使用它。AddWithValue(键[I],值[I])
不是类型安全的。我不建议使用它。如果设置NOCOUNT为开,它也会返回-1
以前是在会话上执行的或在服务器级别设置的。NOCOUNT
是罪魁祸首,谢谢。我们将采用.Parameters.Add(…)
作为正在考虑的备选方案。非常有帮助!
sp_configure 'user options' 0