Asp.net中利用ExecuteNonQuery()执行存储过程返回-1解决方案
Asp.net中利用ExecuteNonQuery()执行存储过程返回-1解决方案
2009-08-22 14:06 by chenkai, 544 阅读, 1 评论, 收藏, 编辑 Asp.net中利用ExecuteNonQuery()方法执行sql语句,并返回影响行数,注意sql语句针对的是增删改三个操作,返回影响行数总是-1:在MSDN上这样解释如果不是同一类型(增删改)的sql语句 执行操作返回-1.
当前存储过程如下:
1
--创建个人插入Proc
2
--分开参数实现
3
create proc AddPersion_Proc
4
@name varchar(80),
5
@password varchar(20),
6
@realname varchar(20),
7
@contractadd varchar(100),
8
@phonenum varchar(24),
9
@email varchar(50),
10
@utypeid varchar(50)--用户类型
11
as
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
set nocount on--不返回影响行数
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
declare @gettypeid int
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
select @gettypeid=id from usertype where utype=@utypeid
18
if(@utypeid='个人用户')
19
insert into userinfor (typeid,username,password,realname,email,address,telenum)
20
values(@gettypeid,@name,@password,@realname,@email,@contractadd,@phonenum)
21![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
--End AddPersion_proc
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
执行结果返回到程序总是显示为-1,虽然返回为-1但数据仍然能够插入,数据库中添加一条记录
其
实问题关键是在这:数据库能够在为我们执行增删改操作并返回影响行数前提是: 默认的 Set Nocount off
是开启的,上面纯粹出于个人习惯直接写了 set nocount on 这样导致无法返回整个存储过程执行后的结果,直接删除set nocount
on使用默认情况的下设置,即能返回显示影响的行数.