[英]I have to retrieve a value from sql server 2008 database
i have created my database as follows : 我创建了我的数据库,如下所示:
userid, password, type
Now in my login.aspx.cs
I want to code such that if userid
and password are matching and a user belongs to the type U then it will go to userpage
and if type is A then go to admin page. 现在在我的login.aspx.cs
我想编码,如果userid
和密码匹配,用户属于U类型,那么它将转到用户userpage
,如果类型是A,则转到管理页面。 The code is shown here but how to send it to the type. 代码显示在此处,但如何将其发送到该类型。 I am having confusion of how to retrieve and compare and then redirect it to the following page. 我对如何检索和比较,然后将其重定向到下一页感到困惑。
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = TextBox1.Text;
string pass = TextBox2.Text;
string utp;
string connectionString = WebConfigurationManager.ConnectionStrings["newdb"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string qry = "select * from login where uid=@username and pass=@pass";
SqlCommand cmd = new SqlCommand(qry,con);
cmd.Parameters.AddWithValue("@username",username);
cmd.Parameters.AddWithValue("@pass",pass);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
if (dt.Rows.Count > 0)
Response.Redirect("http://localhost:55575/WebSite13/admin/Adminpage.aspx");
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
}
You're almost there. 你快到了。 You just need to work this this a bit: 你只需要这样做:
if (dt.Rows.Count > 0)
Response.Redirect("http://localhost:55575/WebSite13/admin/Adminpage.aspx");
That code says that if there are any rows in the database that matched my query, which was a user with the specified user name and password, then redirect. 该代码表示如果数据库中有任何与我的查询匹配的行(具有指定用户名和密码的用户),则重定向。 However, that's not really what you want to do is it? 但是,那不是你真正想做的事情吗? You'd like to check that type. 你想检查那种类型。 So let's modify that a bit: 所以让我们稍微修改一下:
bool hasRows = dt.Rows.Count > 0;
string type = hasRows ? string.Empty : dt.Rows[0].Field<string>("type");
if (hasRows && type == "A")
Response.Redirect("http://localhost:55575/WebSite13/admin/Adminpage.aspx");
else if (hasRows && type == "U")
Response.Redirect("http://localhost:55575/WebSite13/admin/userpage.aspx");
After you check: 检查后:
if(dt.Rows.Count > 0)
add another check to see 添加另一个检查以查看
if(dt.Rows[0]["type"].ToString().Equals("A"))
If true, go to the admin page, else go to the user page. 如果为true,请转到管理页面,否则转到用户页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.