SqlCommand.ExecuteNonQuery() 总是 returns 0
SqlCommand.ExecuteNonQuery() always returns 0
我正在尝试通过我的 C# 代码处理 UPDATE 语句。我使用以下...
using (SqlCommand cmd = new SqlCommand(query, sqlConn))
{
cmd.Parameters.AddWithValue("@CUSTOMER", intCustomer);
cmd.Parameters.AddWithValue("@CONDITION", strCondition);
cmd.Parameters.AddWithValue("@BOOK", strBook);
cmd.Parameters.AddWithValue("@PAGE", strPage);
cmd.Parameters.AddWithValue("@ENDPAGE", strEndPage);
System.Diagnostics.Debug.WriteLine("Expanded query: " +
query.ExpandSqlQuery(cmd.Parameters));
int affectedRows = cmd.ExecuteNonQuery();
System.Diagnostics.Debug.WriteLine("Number of rows affected: " + affectedRows);
}
为了调试,我尝试使用和不使用参数来执行此操作,并且出于某种原因总是得到 0
的 return 值。
我做了一个小的扩展方法来扩展参数以显示实际查询(没有变量)...
public static string ExpandSqlQuery(this String input, SqlParameterCollection sqlParams)
{
string results = input;
foreach (SqlParameter p in sqlParams)
results = results.Replace(p.ParameterName, p.Value.ToString());
return results;
}
... 我在执行查询之前调用它以查看 运行.
System.Diagnostics.Debug.WriteLine("Expanded query: " +
query.ExpandSqlQuery(cmd.Parameters));
然后我采用相同的查询,在 c# 中 returned 0 受影响的行,并在 Microsoft SQL Server Management Studio
中手动 运行 它告诉我 1
行是受影响!
带参数的查询是...
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK'
and Page='@PAGE'
and EndPage='@ENDPAGE'
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK';
展开的查询是...
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=85
and Condition='old'
and Book='00103'
and Page='00304'
and EndPage='00304'
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=85
and Condition='old'
and Book='00103';
有什么办法可以调试这个问题吗?
您不得在参数周围加上引号。因此:
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK'
and Page='@PAGE'
and EndPage='@ENDPAGE'
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK';
应该改为:
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=@CUSTOMER
and Condition=@CONDITION
and Book=@BOOK
and Page=@PAGE
and EndPage=@ENDPAGE
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=@CUSTOMER
and Condition=@CONDITION
and Book=@BOOK;
我正在尝试通过我的 C# 代码处理 UPDATE 语句。我使用以下...
using (SqlCommand cmd = new SqlCommand(query, sqlConn))
{
cmd.Parameters.AddWithValue("@CUSTOMER", intCustomer);
cmd.Parameters.AddWithValue("@CONDITION", strCondition);
cmd.Parameters.AddWithValue("@BOOK", strBook);
cmd.Parameters.AddWithValue("@PAGE", strPage);
cmd.Parameters.AddWithValue("@ENDPAGE", strEndPage);
System.Diagnostics.Debug.WriteLine("Expanded query: " +
query.ExpandSqlQuery(cmd.Parameters));
int affectedRows = cmd.ExecuteNonQuery();
System.Diagnostics.Debug.WriteLine("Number of rows affected: " + affectedRows);
}
为了调试,我尝试使用和不使用参数来执行此操作,并且出于某种原因总是得到 0
的 return 值。
我做了一个小的扩展方法来扩展参数以显示实际查询(没有变量)...
public static string ExpandSqlQuery(this String input, SqlParameterCollection sqlParams)
{
string results = input;
foreach (SqlParameter p in sqlParams)
results = results.Replace(p.ParameterName, p.Value.ToString());
return results;
}
... 我在执行查询之前调用它以查看 运行.
System.Diagnostics.Debug.WriteLine("Expanded query: " +
query.ExpandSqlQuery(cmd.Parameters));
然后我采用相同的查询,在 c# 中 returned 0 受影响的行,并在 Microsoft SQL Server Management Studio
中手动 运行 它告诉我 1
行是受影响!
带参数的查询是...
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK'
and Page='@PAGE'
and EndPage='@ENDPAGE'
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK';
展开的查询是...
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=85
and Condition='old'
and Book='00103'
and Page='00304'
and EndPage='00304'
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=85
and Condition='old'
and Book='00103';
有什么办法可以调试这个问题吗?
您不得在参数周围加上引号。因此:
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK'
and Page='@PAGE'
and EndPage='@ENDPAGE'
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=@CUSTOMER
and Condition='@CONDITION'
and Book='@BOOK';
应该改为:
UPDATE
BookList
SET
Overdue=2
WHERE
Customer=@CUSTOMER
and Condition=@CONDITION
and Book=@BOOK
and Page=@PAGE
and EndPage=@ENDPAGE
and Overdue=1;
UPDATE
BookInfo
SET
Finished=0
WHERE
Customer=@CUSTOMER
and Condition=@CONDITION
and Book=@BOOK;