asp.net自己写代码实现用户和密码登陆时,怎么关联在SQL数据库中的用户和密码?
4个回答
展开全部
把你的用户名和密码输入以后,查询数据库里的用户表!
下面是一个普通的登录验证的代码!
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("","<script>alert('无此用户')</script>");
}
}
}
下面是一个普通的登录验证的代码!
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("","<script>alert('无此用户')</script>");
}
}
}
展开全部
查询语句:select count(*) from biao where name='name' and pass='pass'
datatable dt=查询结果;
if(dt.rows[0][0].tostring!="0")
{
登陆成功
}
datatable dt=查询结果;
if(dt.rows[0][0].tostring!="0")
{
登陆成功
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以DataReader、
if(dr.Read())
{
//登录成功
}
if(dr.Read())
{
//登录成功
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在登陆按钮里写方法,将画面输入的用户名和密码当查询条件。取到数据了说明用户存在,取不到就报错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询