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

本文详细介绍了使用ASP.NET和ADO.NET技术实现登录系统的步骤,并通过代码实现和优化了登录功能,包括用户验证、密码匹配及错误提示。

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;">
                                &nbsp;</td>
                            <td style="width: 188px; text-align: center;">
                    <asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" Font-Size="9pt" />
                                &nbsp;&nbsp;
                    </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图书信息管理系统学习版 系统功能: (1)系统分两种身份:管理员和读者,读者可以分为不同的类型,每种类型借书天数和续借天数都不一样! (2)图书管理:添加图书类别,维护图书类别,登记图书信息,维护图书信息 (3)读者管理:添加读者类别,维护读者类别,登记读者信息,维护读者信息 (4)借阅管理:读者借书登记,读者还书登记,借书超期信息查看! (5)系统管理:管理员和读者登陆系统后可以修改自己的登陆密码,读者可以修改自己的个人信息。 系统特点: (1)根据不同的身份友好显示不同的功能菜单。 (2)程序使用三层架构思想,采用完全面向对象的思想方法设计。 @更多@ http://cleopard.download.csdn.net/ 福利: http://xuemeilaile.com 13份WPF经典开发教程 http://download.csdn.net/album/detail/1115 C#资料合辑二[C#桌面编程入门篇] http://download.csdn.net/album/detail/957 C#资料合辑一[C#入门篇] http://download.csdn.net/album/detail/669 [Csharp高级编程(第6版)](共8压缩卷) http://download.csdn.net/album/detail/667 10个[精品资源]Java学习资料合辑[一] http://download.csdn.net/album/detail/663 10个C#Socket编程代码示例 http://download.csdn.net/album/detail/631 6份GDI+程序设计资源整合[全零分] http://download.csdn.net/album/detail/625 2014年移动游戏行业数据分析 http://download.csdn.net/detail/cleopard/8340331 一文读懂2014年全球互联网广告新生态 http://download.csdn.net/detail/cleopard/8340303
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值