![](/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.