1. 程式人生 > >一個簡單的ASP.NET +ACCESS 登入

一個簡單的ASP.NET +ACCESS 登入

首頁:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width:600; text-align: left;">
        <tr>
            <td colspan="3" style="height: 77px">
                <asp:Label ID="Label1" runat="server"></asp:Label>
            </td>
        </tr>
         <tr>
             <td colspan="3">
                 <span style="font-size: 24pt; color: #ff0000">已是會員,請在這裡登入</span></td>
         </tr>
         <tr>
             <td style="width: 116px; height: 39px;">
                 <span style="font-size: 16pt">
                 使用者名稱:</span></td>
             <td colspan="2" style="height: 39px">
                 <asp:TextBox ID="TextBox1" runat="server" Width="215px"></asp:TextBox></td>
         </tr>
         <tr>
             <td style="width: 116px; height: 47px;">
                 <span style="font-size: 16pt">
                 密碼:</span></td>
             <td colspan="2" style="height: 47px">
                 <asp:TextBox ID="TextBox2" runat="server" Width="214px"></asp:TextBox></td>
         </tr>
         <tr>
             <td style="width: 116px">
             </td>
             <td style="width: 138px">
                 <asp:Button ID="Button1" runat="server" Font-Size="14pt" Text="登 錄" Width="89px" OnClick="Button1_Click" /></td>
             <td style="width: 134px">
             </td>
         </tr>
</table>
</div>
    </form>
</body>
</html>

登陸後臺功能:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("/WebSite21/App_Data/news.mdb");
        OleDbConnection oleConnection = new OleDbConnection(ConnStr);
        oleConnection.Open();
        OleDbCommand mycommand = new OleDbCommand("select * from login where username='" + TextBox1.Text + "' and userpwd='" + TextBox2.Text + "'", oleConnection);
        int count = Convert.ToInt32(mycommand.ExecuteScalar());
        if (count > 0)
        {
            Session["name"] = TextBox1.Text;

            Response.Redirect("cg.aspx");
        }
        else
        {
            Label1.Text = "<script language=javascript>alert( '使用者名稱或密碼錯誤')</script>";
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox1.Focus();
        }
        oleConnection.Close();

    }
}

登陸成功後轉向cg.aspx頁面

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class cg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Session["name"]);
    }
}