C# 源代码
1 /// <summary>
2 /// 校验用户
3 /// </summary>
4 /// <param name="user">用户信息</param>
5 /// <returns></returns>
6 public static int VerifyUser(clsUser user)
7 {
8 int iRet;
9 string sql = String.Format("EXECUTE VerifyUser @myVerifyReader OUTPUT,'{0}','{1}'",user.username,user.password); //执行的T-SQL串
10 SqlCommand scmd = new SqlCommand(sql, conn);
11 scmd.Parameters.Add(new SqlParameter("@myVerifyReader",SqlDbType.Int));
12 scmd.Parameters["@myVerifyReader"].Direction = ParameterDirection.Output;
13 //
14
15 DBOpen(conn);
16 //在返回值上有问题
17 scmd.ExecuteNonQuery();
18 iRet = (int)scmd.Parameters["@myVerifyReader"].Value;
19 DBClose(conn);
20 return iRet;
21 }
2 /// 校验用户
3 /// </summary>
4 /// <param name="user">用户信息</param>
5 /// <returns></returns>
6 public static int VerifyUser(clsUser user)
7 {
8 int iRet;
9 string sql = String.Format("EXECUTE VerifyUser @myVerifyReader OUTPUT,'{0}','{1}'",user.username,user.password); //执行的T-SQL串
10 SqlCommand scmd = new SqlCommand(sql, conn);
11 scmd.Parameters.Add(new SqlParameter("@myVerifyReader",SqlDbType.Int));
12 scmd.Parameters["@myVerifyReader"].Direction = ParameterDirection.Output;
13 //
14
15 DBOpen(conn);
16 //在返回值上有问题
17 scmd.ExecuteNonQuery();
18 iRet = (int)scmd.Parameters["@myVerifyReader"].Value;
19 DBClose(conn);
20 return iRet;
21 }
SQL SERVER存储过程
------------------------------
/*
* VerifyUser 存储过程
* 用途:验证用户登陆
*/
CREATE PROCEDURE VerifyUser
(
@myVerifyReader int OUTPUT, --返回结果
@username varchar(50), --用户名
@userpassword varchar(50) --用户密码
)
AS
IF EXISTS(SELECT [id] FROM [Users] WHERE username = @username AND userpassword = @userpassword)
SET @myVerifyReader = 0 --通过验证
ELSE IF EXISTS(SELECT [id] FROM [Users] WHERE username = @username)
SET @myVerifyReader = 1 --用户存在,密码不正确
ELSE
SET @myVerifyReader = 2 --用户不存在
RETURN isnull(@myVerifyReader,3)
-------------------------------------------------
GO
/*
* VerifyUser 存储过程
* 用途:验证用户登陆
*/
CREATE PROCEDURE VerifyUser
(
@myVerifyReader int OUTPUT, --返回结果
@username varchar(50), --用户名
@userpassword varchar(50) --用户密码
)
AS
IF EXISTS(SELECT [id] FROM [Users] WHERE username = @username AND userpassword = @userpassword)
SET @myVerifyReader = 0 --通过验证
ELSE IF EXISTS(SELECT [id] FROM [Users] WHERE username = @username)
SET @myVerifyReader = 1 --用户存在,密码不正确
ELSE
SET @myVerifyReader = 2 --用户不存在
RETURN isnull(@myVerifyReader,3)
-------------------------------------------------
GO
先简单写一下吧,就是这么用的。
摘自:http://www.cnblogs.com/lizhi/archive/2007/07/10/812336.html
C#获取存储过程返回值和输出参数值
怎样执行存储过程,返回一个记录集 和一个返回值?
比如 CREATE PROCEDURE aaaa
(
@ret int OUTPUT
)
AS
select * from table1
set @ret=0
GO
高手支招!
try something like the following, note, you have to close Reader first
cmd = new SqlCommand("aaaa",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ret", SqlDbType.Int);
cmd.Parameters["@ret"].Direction = ParameterDirection.Output;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//
}
reader.Close();
int ret = (int)cmd.Parameters["@ret"].Value;