@TOC解决方案管理器目录结构
c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010
以下是web程序课程后做的期末项目,前端以及页面逻辑结构使用vs搭建,后端服务器采用access数据库连接,仅供参考。欢迎学习交流~Q871501285
解决方案管理器目录结构
下图为项目结构目录,依次创建,css中为网页的层叠样式表,DB文件夹中为数据库文件,四个aspx文件为网站页面。
数据库文件
存放学生基本信息,前端要操作的内容
首页
首页包含功能按钮与查询功能
效果:
功能按钮
查询功能
可查找出数据库的中的数据
首页代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="首页.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="CSS/StyleSheet2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class = 'dingwei'>
<div id='adds'>学生信息管理系统</div>
<div id='height_10'></div>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="点击获取" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="添加学生信息" />
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("DB/Database.mdb");
conn.Open();
string sql = "select * from TB_STU";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader dr = cmd.ExecuteReader();
string str = xie;
Response.Write(str);
Response.Write("<div class = 'dingwei'>");
Response.Write("<div id='adds'>学生信息管理系统</div>");
Response.Write("<div id='height_10'></div>");
Response.Write("</div>");
while (dr.Read())
{
Response.Write("<tabel><tr>");
Response.Write("<td>学号:" + dr["XH"] + "</td><td>姓名:" + dr["XM"] + "</td><td>性别:" + dr["XB"] + "</td><td>专业:" + dr["ZY"] + "</td><td>电话:" + dr["PHONE"] + "</td>");
Response.Write("<td><a href=删除.aspx?id="+dr["XH"]+">删除</a>");
Response.Write("|<a href=修改.aspx?id=" + dr["XH"] + ">编辑</a></td>");
Response.Write("</tr><table>");
}
Response.Write("<a href=添加.aspx>添加新学生信息</a>");
conn.Close();
Response.End();
}
public string xie { get; set; }
protected void Button2_Click(object sender, EventArgs e)
{
// Get response.
var response = base.Response;
// Redirect temporarily.
// ... Don't throw an HttpException to terminate.
response.Redirect("添加.aspx", false);
}
}
}
`
css代码
{
background-color:#CCFF99;
}
#add
{
height:250px;
width:220px;
margin:0 auto;
margin-top:200px;
}
#adds
{
top:200px;
font-size:2 2px;
color:Blue;}
#height_10
{
width:100%;
height:5px;
background:blue;}
#Button1
{
margin-left:56px;
}
添加页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="添加.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="CSS/StyleSheet1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="adds">添加学生信息</div>
<div id="height_10"></div>
<form id="form2" runat="server">
<div id="add">
学号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
姓名:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
性别:<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Flow"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="0">男</asp:ListItem>
<asp:ListItem Value="1">女</asp:ListItem>
</asp:RadioButtonList>
<br />
<br />
专业:<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" AppendDataBoundItems="True">
<asp:ListItem>计算机科学与技术</asp:ListItem>
<asp:ListItem>物联网工程</asp:ListItem>
<asp:ListItem>电子科学与技术</asp:ListItem>
<asp:ListItem>电子通信</asp:ListItem>
<asp:ListItem>软件工程</asp:ListItem>
</asp:DropDownList>
<br />
<br />
手机:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="返回" onclick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string XH = TextBox1.Text;
string XM = TextBox2.Text;
string XB = RadioButtonList1.SelectedItem.Text;
string ZY = DropDownList1.SelectedItem.Text;
string PHONE = TextBox3.Text;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("DB/Database.mdb");
conn.Open();
string sql = "INSERT INTO TB_STU ( XH , XM , XB , ZY , PHONE ) " + "VALUES ( '" + XH + "', '" + XM + "' , '" + XB + "' , '" + ZY + "', '" + PHONE + "')";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
Label1.Text = "数据添加成功!";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
// Get response.
var response = base.Response;
// Redirect temporarily.
// ... Don't throw an HttpException to terminate.
response.Redirect("首页.aspx", false);
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
删除页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="删除.aspx.cs" Inherits="WebApplication2.删除" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="background-color:#CCFF99;">
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="返回" />
</div>
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace WebApplication2
{
public partial class 删除 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"].ToString();
Label1.Text = "学号为"+id+"学生信息已删除";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("DB/Database.mdb");
conn.Open();
string sql = "Delete FROM TB_STU Where XH = '"+id+"'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
// Get response.
var response = base.Response;
// Redirect temporarily.
// ... Don't throw an HttpException to terminate.
response.Redirect("首页.aspx", false);
}
}
}
更改页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="修改.aspx.cs" Inherits="WebApplication2.修改" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#Button1
{
margin-left:56px;
}</style>
</head>
<body style="background-color:#CCFF99;">
<form id="form1" runat="server">
<div>
学号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
姓名:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
性别:<asp:RadioButtonList
ID="RadioButtonList1" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:RadioButtonList>
<br />
<br />
专业:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>计算机科学与技术</asp:ListItem>
<asp:ListItem>物联网工程</asp:ListItem>
<asp:ListItem>电子科学与技术</asp:ListItem>
<asp:ListItem>电子通信</asp:ListItem>
<asp:ListItem>软件工程</asp:ListItem>
</asp:DropDownList>
<br />
<br />
手机:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="修改" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="返回" onclick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace WebApplication2
{
public partial class 修改 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string XH = Request.QueryString["id"].ToString();
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("DB/Database.mdb");
conn.Open();
string sql = "select XH from TB_STU where XH='"+XH+"'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
TextBox1.Text = XH;
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string XH = TextBox1.Text;
string XM = TextBox2.Text;
string XB = RadioButtonList1.SelectedItem.Text;
string ZY = DropDownList1.SelectedItem.Text;
string PHONE = TextBox3.Text;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("DB/Database.mdb");
conn.Open();
string sql2 = "update TB_STU set XM='"+XM+"'where XH = '"+XH+"'";
string sql3 = "update TB_STU set XB='" + XB + "' where XH = '" + XH + "'";
string sql4 = "update TB_STU set ZY='" + ZY + "' where XH = '" + XH + "'";
string sql5 = "update TB_STU set PHONE='" + PHONE + "' where XH = '" + XH + "'";
OleDbCommand cmd2 = new OleDbCommand(sql2, conn);
cmd2.ExecuteNonQuery();
OleDbCommand cmd3 = new OleDbCommand(sql3, conn);
cmd3.ExecuteNonQuery();
OleDbCommand cmd4 = new OleDbCommand(sql4, conn);
cmd4.ExecuteNonQuery();
OleDbCommand cmd5 = new OleDbCommand(sql5, conn);
cmd5.ExecuteNonQuery();
Label1.Text = "数据修改成功!";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
// Get response.
var response = base.Response;
// Redirect temporarily.
// ... Don't throw an HttpException to terminate.
response.Redirect("首页.aspx", false);
}
}
}
结语
本次设计代码规整,完美实现增删改查操作,前端可以进行自定义调整。