简体   繁体   English

ExecuteNonQuery()不会在C#和SQL Server数据库中返回受影响的行

[英]ExecuteNonQuery() does not return the affected rows in C# and SQL Server database

I'm using SQL Server 2008 and Visual Studio 2010. The issue happen when using ExecuteNonQuery() command. 我正在使用SQL Server 2008和Visual Studio2010。使用ExecuteNonQuery()命令时会发生此问题。 In most cases, it returns the real number of rows affected, but when I tried to delete a group of records, it returns 0. 在大多数情况下,它返回受影响的实际行数,但是当我尝试删除一组记录时,它返回0。

When I opened the table in SQL Server Management Studio, I see 33 records were deleted. 当我在SQL Server Management Studio中打开表时,我看到33条记录已被删除。

This is my code: 这是我的代码:

sqlQuery = "delete from  priv_  where user_id=" + txt_id.Text;
cmd = new SqlCommand(sqlQuery, con);
MessageBox.Show("rows affected = " + cmd.ExecuteNonQuery());

The message box show 消息框显示

rows affected = 0

When I use the same query in SQL Server Management Studio, it shows 当我在SQL Server Management Studio中使用相同的查询时,它显示

33 rows affected

I have change the ONCOUNT to off, but nothing change. 我已将ONCOUNT更改为off,但没有任何变化。

I was experiencing this problem for few weeks.I had a problem with the login rights that I was using for the database. 我遇到这个问题已有几个星期了,我使用的数据库登录权限有问题。 I was able to run queries but not from my application and it does not throw any error. 我能够运行查询,但是无法从我的应用程序中运行,它不会引发任何错误。 So I added the 'owner' rights to the login in SQL Server 2014 under 所以我在SQL Server 2014中的登录名下添加了“所有者”权限

My_server->Security->logins->My_login->Properties->User Mappings->My_database->Select appropriate rights My_server->安全性->登录名-> My_login->属性->用户映射-> My_database->选择适当的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM