我的项目中有一个登录页面,该页面正常运行。我的数据库(SQL Server)有4个字段,其中最后一个是“ permission”,其中有2个值:Admin,User。
我不知道如何配置代码,以便登录后(如果您是admin)将您转移到admin页面,否则转到用户页面。这意味着,如果数据库中用户的“权限”字段为“管理员”,则转移到管理面板或页面。
我后面的C#代码是:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class content_btn1 : System.Web.UI.Page
{
protected void btnlogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from users_table where Name =@username and Password=@password", con);
cmd.Parameters.AddWithValue("@username", txtuser.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("btn2.aspx");
}
else
{
lbllogindetails.Text = "Invalid User or Password !!";
}
}
}
您可以在if条件中编写此代码
if (dt.Rows.Count > 0)
{
if(dt.Rows[0]["permission"] == "Admin")
{
Response.Redirect("admin.aspx");
}
else
{
Response.Redirect("normal.aspx");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句