您的位置:首页 > 编程语言 > C#

C#中调用输入输出参数的存储过程

2008-08-27 22:15 393 查看
关于存储过程的介绍大家想必也不陌生了吧。大家可以参考《浅谈存储过程》一文,这里不做介绍。

今天要介绍的是在C#里如何调用带输入输出参数的存储过程。譬如实现简单的登陆功能,根据用户输入的用户名、用户密码及用户权限实现登陆。这里充当输入参数的有:用户名、用户密码、登陆权限。充当输出参数的就是在数据库里面根据输入参数的信息查询数据库中是否有记录。具体来说就是记录的行数。

数据库中的存储过程构建代码:

use Hotel --数据库名

2if exists(select * from sysobjects where name='proc_userinfo')--判断是否存在存储过程

3drop proc proc_userinfo --若存在删除此存储过程

4go

5--创建存储过程

6create proc proc_userinfo

7@username varchar(50),--输入参数

8@pwd varchar(50),--输入参数

9@grade varchar(20),--输入参数

10@count int output --输出参数output不能少

11--sql语句

12as

13select @count=count(*) from userinfo where username=@username and pwd=@pwd and @grade=grade

14go

15

16

17执行存储过程

18declare @n int

19exec proc_userinfo admin,123,管理员,@n output

20--output不能少

21print @n

22go

数据访问层代码

//调用存储过程执行类似于

2//select count(*) from userinfo where username=username and pwd=pwd and grade=grade

3//接受三个参数分别用来表示用户名、用户密码、用户权限

4public bool GetUserinfo(string username,string pwd,string grade)

5//用dataGradeView.DataSource dataGradeView的数据源创建一个DataTable 对象 需要强转成DataTable

2

3 DataTable dt=(DataTable )dataGradeView.DataSource;

4 //dataGradeView为具体的dataGradeView

5

6 dt.Rows.Clean(); //删除表中的行

7

8 dataGradeView.DataSource=dt;

9//再把dt绑定到dataGradeView上即可

10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: