1.创建access数据库 login.mdb 新建表user
2. login.aspx 页面
代码:
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("");
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("");
}
else
Response.Write("");
}
}
}