当前位置:Gxlcms > 数据库问题 > SQLCommand.ExecutenonQuery受影响的行数为什么会是-1?

SQLCommand.ExecutenonQuery受影响的行数为什么会是-1?

时间:2021-07-01 10:21:17 帮助过:2人阅读

对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。


当我们用executenonquery执行select语句的时候于是就有了返回值为-1,回滚发生在事务中,如果执行SQL不包含事务,可以不做考虑。有事务的话,就要看看是哪里出错了,导致提交不成功,事务回滚。

下面看一下这个Demo

 1 private int Select(string selectqueryString,
 2     string connectionString)
 3 {
 4     using (SqlConnection connection = new SqlConnection(
 5                connectionString))
 6     {
 7         SqlCommand command = new SqlCommand(selectqueryString, connection);
 8         command.Connection.Open();
 9         int result=command.ExecuteNonQuery();
10         return result;
11     }
12 }
13 //result 的值为-1
14 private int Common(string queryString,
15     string connectionString)
16 {
17     using (SqlConnection connection = new SqlConnection(
18                connectionString))
19     {
20         SqlCommand command = new SqlCommand(queryString, connection);
21         command.Connection.Open();
22         int result= command.ExecuteNonQuery();
23         return result;
24     }
25 }
26 //这里的querystring为update insert delete语句
27 //result的值为受影响的行数

 

SQLCommand.ExecutenonQuery受影响的行数为什么会是-1?

标签:

人气教程排行