![](/img/trans.png)
[英]I have lost SQL Server 2008 database, I need to the database structure back from LINQ schema
[英]I have to retrieve a value from sql server 2008 database
我創建了我的數據庫,如下所示:
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 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>");
}
}
}
你快到了。 你只需要這樣做:
if (dt.Rows.Count > 0)
Response.Redirect("http://localhost:55575/WebSite13/admin/Adminpage.aspx");
該代碼表示如果數據庫中有任何與我的查詢匹配的行(具有指定用戶名和密碼的用戶),則重定向。 但是,那不是你真正想做的事情嗎? 你想檢查那種類型。 所以讓我們稍微修改一下:
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");
檢查后:
if(dt.Rows.Count > 0)
添加另一個檢查以查看
if(dt.Rows[0]["type"].ToString().Equals("A"))
如果為true,請轉到管理頁面,否則轉到用戶頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.