我使用用户名和密码登录,这是我在DAL中的代码
public static SqlDataReader login(SchoolBAL bal)
{
SqlConnection con = DBConnection.OpenConnection();
try
{
int i;
SqlCommand cmd1 = new SqlCommand("login", con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@username", bal.UserName);
cmd1.Parameters.AddWithValue("@password", bal.Password);
SqlDataReader dr = cmd1.ExecuteReader();
return dr;
}catch (Exception)
{
throw;
}
}
但我想在这里返回一个整数,我该怎么办呢?
EDIT ::
Create procedure login
(@username varchar (50),@password varchar (50))
as
begin
select UserName,password from tblLogin where UserName=@username and password=@password
end
答案 0 :(得分:1)
您将返回DataReader
的引用,但返回类型的方法为int
。更改方法的返回类型。
public static bool login(SchoolBAL bal)
{
bool isFound=false;
using(SqlConnection con = DBConnection.OpenConnection())
{
using(SqlCommand cmd1 = new SqlCommand("login", con))
{
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@username", bal.UserName);
cmd1.Parameters.AddWithValue("@password", bal.Password);
con.Open();
SqlDataReader dr = cmd1.ExecuteReader();
if(dr.Read())
isFound=true;
dr.Close();
con.Close();
}
}
return isFound;
}
答案 1 :(得分:1)
答案 2 :(得分:0)
public static int login(SchoolBAL bal)
{
SqlConnection con = DBConnection.OpenConnection();
try
{
int i;
SqlCommand cmd1 = new SqlCommand("login", con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@username", bal.UserName);
cmd1.Parameters.AddWithValue("@password", bal.Password);
i = cmd1.ExecuteScalar();
return i;
}
catch (Exception)
{
throw;
}
}