我为简单的用户登录功能编写了一个存储过程。
USE [project]
GO
/****** Object: StoredProcedure [dbo].[authenticateLogin] Script Date: 10/18/2013 9:24:57 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[authenticateLogin] @userName varchar, @password varchar
AS
BEGIN
Declare @userRole int;
Declare @uPassword varchar;
Declare @uRole int;
SET @uPassword = (SELECT uPassword FROM [user] WHERE uName=@userName);
IF(@uPassword = @password)
SET @userRole = (SELECT uRole FROM [user] WHERE uName=@userName);
ELSE
SET @userRole = 0;
--RETURN @userRole
SELECT @userRole
END
我从我的程序中这样调用它:
internal int Authenticate(string userName, string password)
{
int userRole = 0;
Shared shrObj=new Shared();
string encPassword = shrObj.EncryptToString(password);
SqlConnection sqlCon = Shared.GetSqlCon();
var sqlCom = new SqlCommand("authenticateLogin", sqlCon);
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom.Parameters.AddWithValue("@userName", userName);
sqlCom.Parameters.AddWithValue("@password", encPassword);
try
{
userRole = Shared.ExecuteNonQueryOnProcedure(sqlCon, sqlCom);
if (userRole > 0) return userRole;
}
catch (SqlException sqlEx)
{
//catch
}
finally
{
Shared.COC(sqlCon);
}
return userRole;
}
在Shared.cs
public static int ExecuteNonQueryOnProcedure(SqlConnection sqlCon, SqlCommand sqlCom) {
int rowCount = 0;
SqlParameter returnParameter = sqlCom.Parameters.Add("userRole", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
try
{
sqlCon.Open();
//rowCount = sqlCom.ExecuteNonQuery();
var inu = sqlCom.ExecuteScalar().ToString();
}
catch (Exception ex)
{
//catch
return rowCount;
}
return (int)returnParameter.Value;
}
但问题是即使提供的参数匹配,它也总是返回我 0。请帮助我我在哪里做错了。