asp.net连接access数据库的登录页面
来源:互联网 发布:手机破解软件 编辑:程序博客网 时间:2024/05/20 06:05
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>");
}
}
}
- asp.net连接access数据库的登录页面
- Asp.net连接Access数据库的方法
- ASP.NET 连接 ACCESS 数据库的代码
- asp.net连接Access数据库
- asp.net连接Access数据库
- asp.net连接Access数据库
- asp.net连接Access数据库
- ASP.NET连接Access数据库
- ASP.Net 连接ACCESS数据库
- ASP.NET连接Access数据库
- ASP.NET连接Access数据库
- asp.net连接Access数据库
- ASP.net连接access数据库
- asp.net连接Access数据库
- asp.net连接Access数据库
- ASP.NET连接Access数据库
- asp.net 连接access数据库
- ASP.NET连接Access数据库
- 读书笔记MoreEffectiveC++(16)
- Gdb调试多进程程序
- OC属性
- 【Tomcat】Tomcat报错追踪
- 判断两三角形是否相交
- asp.net连接access数据库的登录页面
- Struts学习总结之配置文件
- CodeForces 463C - Gargari and Bishops
- SAP的实施公司有哪些?
- 简单实现listview可选择的列表(不用重写adapter)
- 关于在MathType中任意维度矩阵的输入方式
- Android获取屏幕宽度和高度
- 可变字典最便捷的设置键值对方式
- Java启动线程的方法