我創建了我的數據庫如下:我不得不從SQL Server檢索值2008數據庫
userid, password, type
現在在我的login.aspx.cs
我想代碼,這樣如果userid
和密碼匹配和用戶屬於U型,那麼它將去userpage
,如果類型是A然後去管理頁面。代碼顯示在這裏,但如何將其發送到該類型。我很困惑如何檢索和比較,然後將其重定向到下一頁。
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 [email protected] and [email protected]";
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>");
}
}
}
類型列將在'DataTable dt'中(應該是第一行也是唯一的一行); 'dt.Rows [0] [「type」]。ToString()'會給你的值。不過,我會推薦使用'SqlDataReader'來代替創建整個'DataTable'。 – Tim