存储过程中完成更新操作后,使用@@rowcount返回影响行数。如果有多条语句,可定义变量保存影响行数。示例如下
ALTER PROCEDURE [dbo].[P_CSM_UpdateHandleResultEmpName] @HandleIndex int=-1 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; update a set a.FromEmpName=b.empname from HandleResult a left join users b on a.FromUserName=b.openid where a.HandleIndex>@HandleIndex and a.FromEmpName is null and b.empname is not null select @@rowcount --这里返回影响行数 END
然后C# 代码中,使用SqlCommand.ExecuteScalar()来执行该存储过程,就能获取@@rowcount,也就是影响行数
代码如下:
private int UpdateHandleResultEmpName() { SqlConnection con = new SqlConnection("你的数据库连接字符串"); SqlCommand com = con.CreateCommand(); com.CommandType = CommandType.StoredProcedure; com.CommandText = "P_CSM_UpdateHandleResultEmpName"; //存储过程名称 con.Open(); int rowcount = Convert.ToInt32(com.ExecuteScalar()); con.Close(); return rowcount; }
C# 存储过程返回影响行数
时间: 2024-10-12 22:38:43