我正在尝试通过我的 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
由于某种原因总是得到返回值。
我做了一个小的扩展方法来扩展参数以向我展示实际的查询(没有变量)......
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# 中返回 0 个受影响行的 SAME EXACT 查询并手动运行它,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';
有什么想法可以解决这个问题吗?