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>");
}
}
}
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 MVC4 项目文件结构及各文件的作用
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET MVC异常处理
- ASP.NET POST XML JSON数据,发送与接收
- asp.net中BeginInvoke和EndInvoke方法使用方法
- The return type is incompatible with JspSourceDependent.getDependants():JasperException问题分析与解决方法
- The return type is incompatible with JspSourceDependent.getDependants():JasperException问题分析与解决方法...
- 搜索引擎关键词劫持之asp篇
- Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)
- 为了树莓派IIraspberrypi安装emacs+ecb+cedet+session+color-theme+cscope+linum
- 动态生成页面(一)——ASP.NET中Literal使用
- Maticsoft.Framework for Asp.Net框架应用实例(一)
- Asp.NET MVC3 使用 SignalR 实现推
- Metasploit测试暴雷漏洞 (编号CVE-2012-1889)
- 《ASP.NET》数据绑定—DataList实践篇
- 利用Metasploit测试一个古老的IE8漏洞(编号CVE-2012-1875)
- ASP、JSP、PHP 三种技术比较
- ASP、JSP、PHP 三种技术比较
- asp.net 弹出框信息
- asp.net重写服务器控件