登录验证不适用于3层archuitecture [英] Login verification is not working with 3 tier archuitecture
问题描述
任何输入它登录到应用程序。
数据层:
with any inputs it is logging into application.
Data layer:
public DataSet Userlogin(Users objlogin)
{
SqlConnection con = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand("SELECT * FROM User_details WHERE [Login Id]=@loginid AND [Password]=@password", con);
cmd.Parameters.AddWithValue("@loginid", objlogin.loginId);
cmd.Parameters.AddWithValue("@password", objlogin.Password);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
//DataTable dt = new DataTable();
//da.Fill(dt);
//return (dt);
}
BLL:
BLL:
public DataSet Userlogin(Users objUser)
{
DataLayer objUserDAL = new DataLayer();
try
{
return objUserDAL.Userlogin(objUser);
}
catch (Exception ex)
{
throw ex;
}
}
UI图层:
UI layer:
<pre> protected void btnlogin_Click(object sender, EventArgs e)
{
Users objuser = new Users();
objuser.loginId = loginid.Text;
objuser.Password = pwd.Text;
DataSet ds = new DataSet();
//DataTable dt = new DataTable();
BAL objBAL = new BAL();
ds= objBAL.Userlogin(objuser);
if(ds.Tables.Count>0)
{
Session["User"] = loginid.Text;
Response.Redirect("~/Transactions.aspx");
}
else
{
Response.Write("<script>alert('Invalid Credentials!')</script> ");
}
}
}
}
我尝试了什么:
它使用单层以下代码
What I have tried:
It is working with single tier- below code
<pre> protected void btnlogin_Click(object sender, EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("SELECT * FROM User_details WHERE [Login Id]=@loginid AND [Password]=@password",con );
cmd.Parameters.Add("@loginid", SqlDbType.VarChar).Value = loginid.Text;
cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pwd.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
Session["User"] = loginid.Text;
Response.Redirect("Transactions.aspx");
}
else
{
Response.Write("<script>alert('Invalid Credentials!')</script> ");
}
}
推荐答案
第一步是了解代码。
无效的那个
First step is to understand the code.
The one that does not work has
if(ds.Tables.Count>0)
和工作的那个
and the one that does work has
if(dt.Rows.Count > 0)
您需要检查表是否存在,然后检查行是否存在。
You need to check if tables exist and THEN also check if Rows exist.
DataSet
由DataTables
组成,因此您需要指定如下表:
DataSet
is composed ofDataTables
so you need to specify the table like this:
ds.Tables["TableName"].Count > 0
或使用这样的索引:
or using index like this:
ds.Tables[0].Count > 0
虽然有效,但我仍然建议您使用 DataTable
而不是 DataSet
,因为您只处理一个数据库表。另外,养成使用 SqlConnection
, SqlCommand <等资源的
对象
的习惯在using语句中使用/ code>和 SqlDataAdapter
,以确保在使用对象后对象将被正确处理和关闭。这是一个简单的例子:
While what works, I would still recommend you to DataTable
instead of DataSet
since you are only dealing with one database table. Also, make it a habit to put objects
that eat resources such as SqlConnection
, SqlCommand
and SqlDataAdapter
within a using statement to ensure that objects will be properly disposed and closed after they are used. Here's a quick example:
public DataTable Userlogin(Users objlogin){
DataTable dt = new DataTable();
string sqlStatement = "SELECT * FROM User_details WHERE [Login Id]=@loginid AND [Password]=@password";
using(SqlConnection connection = new SqlConnection(GetConnectionString())){
using(SqlCommand cmd = new SqlCommand(sqlStatement ,connection)){
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@loginid", objlogin.loginId);
cmd.Parameters.AddWithValue("@password", objlogin.Password);
using(SqlDataAdapter da = new SqlDataAdapter(cmd)){
da.Fill(dt);
}
}
}
return dt;
}
然后在您的业务层中,您还需要返回一个DataTable,这样您就可以在你的UI层:
Then in your Business layer, you would need to return a DataTable too so you can do something like this in your UI layer:
BAL objBAL = new BAL();
DataTable dt = objBAL.Userlogin(objuser);
if(dt.Rows.Count > 0)
{
Session["User"] = loginid.Text;
Response.Redirect("~/Transactions.aspx");
}
else
{
Response.Write("<script>alert('Invalid Credentials!')</script> ");
}
注意我们现在正在使用 dt.Rows.Count
检查您的查询是否返回任何数据。
Note that we're now using dt.Rows.Count
to check if your query returns any data.
这篇关于登录验证不适用于3层archuitecture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!