您的位置:首页 > 编程语言 > ASP

asp.net连接access数据库的登录页面

2015-07-27 10:58 531 查看
1.创建access数据库 login.mdb 新建表user

2. login.aspx 页面

代码:<div style="height: 33px">

<h2 style="text-align:center">欢迎登陆</h2>

</div>

<table align="center" cellpadding="0" cellspacing="0"

style="height: 148px; width: 382px">

<tr>

<td style="width: 107px; text-align: center;">

<asp:Label ID="labUserID" runat="server" Text="用户ID" Font-Size="9pt" Width="38px"></asp:Label></td>

<td style="width: 188px; text-align: center;">

<asp:TextBox ID="txtUserID" runat="server" Font-Size="9pt"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 107px; text-align: center;">

<asp:Label ID="labUserName" runat="server" Text="用户名" Font-Size="9pt" Width="38px"></asp:Label></td>

<td style="width: 188px; text-align: center;">

<asp:TextBox ID="txtUserName" runat="server" Font-Size="9pt"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 107px; text-align: center;">

<asp:Label ID="labPwd" runat="server" Text="密 码" Font-Size="9pt" Width="38px"></asp:Label></td>

<td style="width: 188px; text-align: center;">

<asp:TextBox ID="txtPassword" runat="server" Font-Size="9pt" TextMode="Password" Width="128px"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 107px; text-align: center;">

 </td>

<td style="width: 188px; text-align: center;">

<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" Font-Size="9pt" />

  

</td>

</tr>

</table>

3.login.aspx.cs后台代码:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

namespace webtext2

{

public partial class login : System.Web.UI.Page

{

public string strConnection;

OleDbConnection myConn;

protected void Page_Load(object sender, EventArgs e)

{

string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\webtext2\\webtext2\\App_Data\\login.mdb"; //数据库存放路径

myConn = new OleDbConnection(strConnection);

}

protected void btnLogin_Click(object sender, EventArgs e)

{

string userid,pwd,username;

userid=txtUserID.Text;

pwd=txtPassword.Text;

username=txtUserName.Text;

string mySel = "select count(*)as iCount from [user] where UId=" + userid + " and UName='" + username + "'and UPassword='" + pwd + "'";

OleDbCommand myCmd = new OleDbCommand(mySel, myConn);

myCmd.Connection.Open();

OleDbDataReader Dr;

Dr = myCmd.ExecuteReader();

Dr.Read();

string Count = Dr["iCount"].ToString(); ;

Dr.Close();

if (Count !="0")

{

Session["UId"] = userid;

Response.Redirect("main.aspx");

}

else

Response.Write("<script lanuage=javascript>alert('用户名或密码错误!');location='javascript:history.go(-1)'</script>");

return;

}

}

}

把用户名错误和密码错误分开:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

namespace webtext2

{

public partial class login : System.Web.UI.Page

{

public string strConnection;

OleDbConnection myConn;

protected void Page_Load(object sender, EventArgs e)

{

string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "E:\\webtext2\\webtext2\\App_Data\\login.mdb"; //数据库存放路径

myConn = new OleDbConnection(strConnection);

}

protected void btnLogin_Click(object sender, EventArgs e)

{

string userid, pwd, username;

userid = txtUserID.Text;

pwd = txtPassword.Text;

username = txtUserName.Text;

string mySel = "SELECT count(*) as iCount FROM [user] where UId=" + userid + " and UName='" + username + "'";

OleDbCommand myCmd1 = new OleDbCommand(mySel, myConn);

myCmd1.Connection.Open();

OleDbDataReader Dr1;

Dr1 = myCmd1.ExecuteReader();

Dr1.Read();

string Count = Dr1["iCount"].ToString();

Dr1.Close();

myCmd1.Connection.Close();

string DrPwd;

if (Count != "0")

{

mySel = "SELECT * FROM [user] where UId=" + userid + " and UName='" + username + "'";

OleDbCommand myCmd = new OleDbCommand(mySel, myConn);

myCmd.Connection.Open();

OleDbDataReader Dr;

Dr = myCmd.ExecuteReader();

Dr.Read();

DrPwd = Dr["UPassword"].ToString(); ;

Dr.Close();

if (DrPwd == pwd)

{

Session["UId"] = userid;

Response.Redirect("main.aspx");

}

else

Response.Write("<script lanuage=javascript>alert('密码错误!');location='javascript:history.go(-1)'</script>");

}

else

Response.Write("<script lanuage=javascript>alert('用户名错误!');location='javascript:history.go(-1)'</script>");

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: