数据库搭建:
资源管理器的内容
Functions.cs文件负责连接数据库,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace EHotelMS2.Models
{
public class Functions
{
private SqlConnection Con;
private SqlCommand Cmd;
private DataTable dt;
private string ConStr;
private SqlDataAdapter sda;
public int setData(string Query)
{
int Cnt;
if(Con.State == ConnectionState.Closed)
{
Con.Open();
}
Cmd.CommandText = Query;
Cnt = Cmd.ExecuteNonQuery();
Con.Close();
return Cnt;
}
public DataTable GetData(string Query)
{
dt = new DataTable();
sda = new SqlDataAdapter(Query, ConStr);
sda.Fill(dt);
return dt;
}
public Functions()
{
ConStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DC2020\Documents\EHotelDb1.mdf;Integrated Security=True;Connect Timeout=30";
//这段内容右键数据库,点击属性,查看连接字符串,前面加上@
Con = new SqlConnection(ConStr);
Cmd = new SqlCommand();
Cmd.Connection = Con;
}
}
}
Login.aspx内容:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="EHotelMS2.View.Login" EnableEventValidation="false"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link rel="stylesheet" href="../Assets/Libraries/Bootstrap/css/bootstrap.min.css" />
<style>
body{
background-image:url(../Assets/Images/hotel4.jpg);
background-size:cover;
}
.container-fluid{
opacity:0.9;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="container-fluid">
<div class="row" style="height:200px"></div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4 bg-light rounded-3">
<h1 class="text-text-success text-center">皇家理工度假酒店</h1>
<form>
<div class="mb-3">
<label for="UserTb" class="form-label">用户名</label>
<input type="text" class="form-control" id="UserTb" runat="server" required="required">
</div>
<div class="mb-3">
<label for="PasswordTb" class="form-label">密码</label>
<input type="password" class="form-control" id="PasswordTb" runat="server" required="required">
</div>
<div class="mb-3">
<label id="ErrMsg" class="text-danger" runat="server"></label>
<input type="radio" id="AdminCb" runat="server" name="Role"><label class="text-success">管理员</label>
<input type="radio" id="UserCb" runat="server" name="Role"><label class="text-success">用户</label>
</div>
<div class="d-grid">
<asp:Button ID="LoginBtn" runat="server" Text="登陆" class="btn btn-success btn-block" OnClick="LoginBtn_Click" />
</div>
<br />
</form>
</div>
<div class="col-md-4"></div>
</div>
</div>
</div>
</form>
</body>
</html>
Login.aspx.cs内容:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EHotelMS2.View
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Con = new Models.Functions();
}
Models.Functions Con;
protected void LoginBtn_Click(object sender, EventArgs e)
{
// Response.Redirect("Admin/Rooms.aspx");
if (AdminCb.Checked)
{
if (UserTb.Value == "Admin" && PasswordTb.Value == "123456")
{
Session["UserName"] = "Admin";
//Session缓存
Response.Redirect("https://localhost:44386/View/Admin/Categories.aspx");
}
else
{
ErrMsg.InnerText = "无效的管理员!!!";
}
}
else
{
string Query = "select UId,UName,UPass from UserTbl where UName='{0}' and UPass='{1}'";
Query = string.Format(Query, UserTb.Value, PasswordTb.Value);
DataTable dt = Con.GetData(Query);
if (dt.Rows.Count == 0)
{
ErrMsg.InnerText = "无效的用户!!!";
}
else
{
Session["UserName"] = dt.Rows[0][1].ToString();
Session["UId"] = dt.Rows[0][0].ToString();
Response.Redirect("https://localhost:44312/View/Users/Booking.aspx");
}
}
}
}
}
Categories.aspx的内容:
%@ Page Title="" Language="C#" MasterPageFile="~/View/Admin/AdminMaster.Master" AutoEventWireup="true" CodeBehind="Categories.aspx.cs" Inherits="EHotelMS2.View.Admin.Categories" EnableEventValidation="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MyBody" runat="server">
<div class="container-fluid">
<div class="row">
<div class = "col-4"></div>
<div class = "col-4"><h1 class="text-success text-center">房型管理</h1></div>
<div class = "col-4"></div>
<div class = "col-4"></div>
</div>
<div class ="row">
<div class="col-md-3">
<form>
<div class= "mb-3">
<label for= "CatNameTb" class= "form-label"> 房型名称 </label>
<input type = "text" class= "form-control" id = "CatNameTb" runat="server">
</div>
<div class= "mb-3">
<label for= "RemarkTb" class= "form-label">标签</label>
<input type = "text" class= "form-control" id = "RemarkTb" runat="server">
</div>
<div class="row">
<div class="col d-grid">
<asp:Button ID="EditBtn" runat="server" Text="编辑" class="btn btn-warning btn-block" OnClick="EditBtn_Click"/>
</div>
<div class="col d-grid">
<asp:Button ID="DeleteBtn" runat="server" Text="删除" class="btn btn-danger btn-block" OnClick="DeleteBtn_Click"/>
</div>
</div>
<br/>
<div class= "d-grid">
<label id ="ErrMsg" runat ="server" class="text-danger"></label>
<asp:Button ID="SaveBtn" runat="server" Text ="保存" CssClass="btn btn-success btn-block " OnClick="SaveBtn_Click"/>
</div>
<br/>
</form>
</div>
<div class = "col-md-9">
<asp:GridView ID="CategoriesGV" runat="server" Class="table" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="CategoriesGV_SelectedIndexChanged" AutoGenerateSelectButton="True">
<AlternatingRowStyle BackColor="White"/>
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" Text="选择"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White"/>
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White"/>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"/>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy"/>
<SortedAscendingCellStyle BackColor="#FDF5AC"/>
<SortedAscendingHeaderStyle BackColor="#4D0000"/>
<SortedDescendingCellStyle BackColor="#FCF6C0"/>
<SortedDescendingHeaderStyle BackColor="#820000"/>
</asp:GridView>
</div>
</div>
</div>
</asp:Content>
Categories.aspx.cs的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EHotelMS2.Models;
namespace EHotelMS2.View.Admin
{
public partial class Categories : System.Web.UI.Page
{
Models.Functions Con;
protected void Page_Load(object sender, EventArgs e)
{
Con = new Models.Functions();
ShowCategories();
}
private void ShowCategories()
{
string Query = "select CatId as Id,CatName as Categories,CatRemarks from CategoryTb1";
CategoriesGV.DataSource = Con.GetData(Query);
CategoriesGV.DataBind();
CategoriesGV.HeaderRow.Cells[1].Text = "序号";
CategoriesGV.HeaderRow.Cells[2].Text = "房型";
CategoriesGV.HeaderRow.Cells[3].Text = "标签";
}
protected void SaveBtn_Click(object sender, EventArgs e)
{
try
{
string CatName = CatNameTb.Value;
string Rem = RemarkTb.Value;
string Query = "insert into CategoryTb1 values('{0}','{1}')";
Query = string.Format(Query, CatName, Rem);
Con.setData(Query);
ErrMsg.InnerText = "房型已添加";
}
catch (Exception Ex) {
ErrMsg.InnerText = Ex.ToString();
}
}
int Key = 0;
protected void CategoriesGV_SelectedIndexChanged(object sender, EventArgs e)
{
Key = Convert.ToInt32(CategoriesGV.SelectedRow.Cells[1].Text);
CatNameTb.Value = CategoriesGV.SelectedRow.Cells[2].Text;
RemarkTb.Value = CategoriesGV.SelectedRow.Cells[3].Text;
}
protected void EditBtn_Click(object sender, EventArgs e)//编辑
{
try
{
string CatName = CatNameTb.Value;
string Rem = RemarkTb.Value;
string Query = "update CategoryTb1 set CatName='{0}',CatRemarks='{1}' where CatId={2}";
Query = string.Format(Query, CatName, Rem, CategoriesGV.SelectedRow.Cells[1].Text);
Con.setData(Query);
ShowCategories();
ErrMsg.InnerText = "房型已更新!";
}
catch (Exception Ex)
{
ErrMsg.InnerText = Ex.Message;
}
}
protected void DeleteBtn_Click(object sender, EventArgs e)
{
try
{
string CatName = CatNameTb.Value;
string Rem = RemarkTb.Value;
string Query = "delete from CategoryTb1 where CatId={0}";
Query = string.Format(Query, CategoriesGV.SelectedRow.Cells[1].Text);
Con.setData(Query);
ShowCategories();
ErrMsg.InnerText = "房型已删除!";
}
catch (Exception Ex)
{
ErrMsg.InnerText = Ex.Message;
}
}
}
}
内容演示:
其他内容提示:
工具-选项,添加如下内容,防止控件没法添加
创建项目的时候,注意
这两个项目需要区分,选VB那个,aspx文件下不会有相关联的.cs文件
这个项目的层次大概是:母版页-包含母版页的web窗体-.cs文件