gpt4 book ai didi

c# - dapper c# 返回找到的行不受影响的行

转载 作者:可可西里 更新时间:2023-11-01 08:02:40 24 4
gpt4 key购买 nike

我有一个简单的存储过程,它在表上进行更新。

CREATE PROCEDURE `test_v1`(
out v_changed_rows int
)
BEGIN
update mytable
set color = 'green'
where id = 964291;
set v_changed_rows= ROW_COUNT();
END

从 mysql workbench 调用这个存储过程会给出受影响的正确行(即第一次它将返回 1,因此返回 0,因为我用相同的值更新它,因此没有变化)

enter image description here

当我从 C# 调用此存储过程时出现问题 enter image description here

我尝试在 for 循环中调用此查询,每次它都返回 1。如何获取受查询影响的行,而不是查询找到的行?谢谢。

最佳答案

ROW_COUNT() 有这样的注释功能:

For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.

虽然它说默认返回实际更改的行数,但对于 MySql .Net 客户端,默认行为是返回与 WHERE 子句匹配的行数。这应该是由于传递给 mysql_real_connectclient_flag 参数值所致.

您可以通过显式设置 UseAffectedRows 来更改此行为在您的连接字符串中设置为 True:

server=localhost;user id=test;password=test;database=test;UseAffectedRows=True

我已经尝试过了,它在 ADO.NET 和 Dapper 上都运行良好。

关于c# - dapper c# 返回找到的行不受影响的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com