asp.net自己写代码实现用户和密码登陆时,怎么关联在SQL数据库中的用户和密码?

2025-01-05 06:24:32
推荐回答(4个)
回答1:

把你的用户名和密码输入以后,查询数据库里的用户表!
下面是一个普通的登录验证的代码!

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

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 uid=txtCardId.Text;
string pwd=TxtPwd.Text;
bool flag = false;
SqlConnection con = new SqlConnection("server=.;database=bank;uid=sa;pwd=sa");
SqlDataAdapter adp = new SqlDataAdapter("select * from bankcard",con);
DataSet ds = new DataSet();
adp.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
string cardid=dr["CardId"].ToString();
string password=dr["Password"].ToString();
if (uid == cardid && pwd == password)
{
flag = true;
break;
}
}
if (flag == true)
{
Session["uid"] = uid;
if (DropDownList1.SelectedValue == "0")
{
Response.Redirect("TransferInfo.aspx");
}
else if (DropDownList1.SelectedValue == "1")
{
Response.Redirect("Transfer.aspx");
}
}
else
{
Page.RegisterStartupScript("","");
}
}
}

回答2:

查询语句:select count(*) from biao where name='name' and pass='pass'
datatable dt=查询结果;
if(dt.rows[0][0].tostring!="0")
{
登陆成功
}

回答3:

可以DataReader、
if(dr.Read())
{
//登录成功
}

回答4:

在登陆按钮里写方法,将画面输入的用户名和密码当查询条件。取到数据了说明用户存在,取不到就报错。