UserAccount

UserID

UserName

PassWord

RegisterTime

RegisterIP

12

6                   

6                   

2012-12-31

6

18

5                   

5                   

2013-01-01

5

19

1                   

1                   

2013-01-01

1

20

2                   

2                   

2013-01-01

2

21

3                   

3                   

2013-01-01

3

22

4                   

4                   

2013-01-01

4

23

5                   

5                   

2013-01-01

5

25

7                   

7                   

2013-01-01

7

26

8                   

8                   

2013-01-01

8

NULL

NULL

NULL

NULL

NULL

针对上面的表,我使用存储过程对它做一些操作:

1. 只返回单一记录集的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountasselect * from UserAccountgo-------------执行上面的存储过程----------------exec GetUserAccount

 

 结果:相当于运行 select * from UserAccount 这行代码,结果为整个表的数据。

2.没有输入输出的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure inUserAccountasinsert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9)go-------------执行上面的存储过程----------------exec inUserAccount

 

 结果:相当于运行 insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9) 这行代码。

3.有返回值的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure inUserAccountReasinsert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(10,10,'2013-01-02',10)return @@rowcountgo-------------执行上面的存储过程----------------exec inUserAccountRe

 

 解释:这里的@@rowcount为执行存储过程影响的行数,执行的结果是不仅插入了一条数据,还返回了一个值即 return value =1  ,这个可以在程序中获取,稍后在c#调用存储过程中会有说到。

4.有输入参数和输出参数的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe@UserName nchar(20),@UserID int outputasif(@UserName>5)select @UserID=COUNT(*) from UserAccount where UserID>25elseset @UserID=1000go-------------执行上面的存储过程----------------exec GetUserAccountRe '7',null

 

 解释:@UserName为输入参数,@UserID为输出参数。 运行结果为@userIDCOOUT*)即 =1

5. 同时具有返回值、输入参数、输出参数的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe1@UserName nchar(20),@UserID int outputasif(@UserName>5)select @UserID=COUNT(*) from UserAccount where UserID>25elseset @UserID=1000return @@rowcountgo-------------执行上面的存储过程----------------exec GetUserAccountRe1 '7',null

 

 结果:@userIDCOOUT*)即 =1Retun Value=1

6.同时返回参数和记录集的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe2@UserName nchar(20),@UserID int outputasif(@UserName>5)select @UserID=COUNT(*) from UserAccount where UserID>25elseset @UserID=1000select * from UserAccountreturn @@rowcountgo-------------执行上面的存储过程----------------exec GetUserAccountRe2 '7',null

 

 结果:返回执行 select * from UserAccount 这句代码的结果集,同时@userIDCOOUT*)即 =1Retun Value=9 

7.返回多个记录集的存储过程 

 

-------------创建名为GetUserAccount的存储过程----------------create Procedure GetUserAccountRe3asselect * from UserAccountselect * from UserAccount where UserID>5go-------------执行上面的存储过程----------------exec GetUserAccountRe3

 

 结果:返回两个结果集,一个为 select * from UserAccount,另一个为 select * from UserAccount where UserID>5 

小结:上面我们创建了各式的存储过程,下面看我们在c#中怎样调用这些存储过程。

 

 

posted on 2015-03-06 15:13  lizihong  阅读(328)  评论(0编辑  收藏  举报