Visual Studio (C#)、Microsoft Access 搭建ASP.NET网站(页面功能概述一)

时间:2024-05-21 06:59:57

一、注册/登录页面

Visual Studio 2017(C#)、Microsoft Access 2010搭建ASP.NET网站(页面功能概述一) 

1、点击登录按钮:

if 用户名=”” or 密码=”” then

消息提示:用户名或密码不正确

Visual Studio 2017(C#)、Microsoft Access 2010搭建ASP.NET网站(页面功能概述一)

Else

根据所填“用户名”和“密码”查询“用户数据库”

If 查询失败 then

消息提示:该户名不存在,请先进行注册

Else

跳转到数据显示页面

Visual Studio 2017(C#)、Microsoft Access 2010搭建ASP.NET网站(页面功能概述一)

2、点击注册按钮

if 用户名=”” or 密码=”” then

消息提示:用户名或密码不能为空

Else

根据所填“用户名”和“密码”查询“用户数据库”

If 查询失败 then

在数据库插入数据

消息提示:注册成功,可以进行登录

Else

消息提示:该用户已存在,请直接登录或重新注册

注:

1、在asp.net中没有messagebox用法,有的时候在执行某个操作的时候希望能够弹出一些信息框:alert弹出的警告信息框,Response.Write("<script>alert('该用户已存在,请直接登录或重新注册!')</script>")

2、插入记录时,在表名和字段名加[].如:insert into [test]([ID],[password])

完整代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.OleDb;

 

public partial class 注册页面 : System.Web.UI.Page

{

    protected void Button1_Click(object sender, EventArgs e)

    {

        //登录按钮

        if(username.Text=="" || password.Text=="")

            Response.Write("<script>alert('用户名或密码有误!')</script>");

        else

        {

            string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:/御用闲人办公网站系统/御用闲人/test.accdb;";

            OleDbConnection OleDbConnection = new OleDbConnection(str);

            OleDbConnection.Open();

         OleDbCommand sqlcmd = new OleDbCommand(@"select * from test where ID='"+this.username.Text +"'and password='"+this.password.Text+"'", OleDbConnection);  //sql语句  

            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())

            {                

                Response.Redirect("数据显示.aspx");

            }

            else

            {

                Response.Write("<script>alert('该用户不存在,请先进行注册!')</script>");

            }

            OleDbConnection.Close();

        }

    } 

    protected void Button2_Click(object sender, EventArgs e)

    {

        //注册按钮

        if (username.Text == "" || password.Text == "")

            Response.Write("<script>alert('用户名或密码有误!')</script>");

        else

        {

        string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:/御用闲人办公网站系统/御用闲人/test.accdb;";

            OleDbConnection OleDbConnection = new OleDbConnection(str);

            OleDbConnection.Open();

            OleDbCommand sqlcmd = new OleDbCommand(@"select * from test where ID='" + this.username.Text + "'and password='" + this.password.Text + "'", OleDbConnection);  //sql语句  

            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())

            {                

                Response.Write("<script>alert('该用户已存在,请重新进行注册或直接登录!')</script>");

            }

            else

            {

              sqlcmd = new OleDbCommand("insert into [test]([ID],[password]) values('" + this.username.Text + "','" + this.password.Text + "')", OleDbConnection);  //sql语句  

                sqlcmd.ExecuteNonQuery();

                Response.Write("<script>alert('注册成功,请进行登录!')</script>");

            }

            OleDbConnection.Close();

        }

    }

}