c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010

@TOC解决方案管理器目录结构

c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010

以下是web程序课程后做的期末项目,前端以及页面逻辑结构使用vs搭建,后端服务器采用access数据库连接,仅供参考。欢迎学习交流~Q871501285

解决方案管理器目录结构

下图为项目结构目录,依次创建,css中为网页的层叠样式表,DB文件夹中为数据库文件,四个aspx文件为网站页面。
c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010_第1张图片

数据库文件

存放学生基本信息,前端要操作的内容
c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010_第2张图片

首页

首页包含功能按钮与查询功能

效果:

功能按钮
c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010_第3张图片
查询功能
可查找出数据库的中的数据
c#web窗体实现对数据库的增删改查,学生管理信息系统~vs2010+access2010_第4张图片

首页代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="首页.aspx.cs" Inherits="WebApplication2.WebForm2" %>





    
    



    
学生信息管理系统

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("
"); Response.Write("
学生信息管理系统
"); Response.Write("
"); Response.Write("
"); while (dr.Read()) { Response.Write(""); Response.Write("学号:" + dr["XH"] + "姓名:" + dr["XM"] + "性别:" + dr["XB"] + "专业:" + dr["ZY"] + "电话:" + dr["PHONE"] + ""); Response.Write("删除"); Response.Write("|编辑"); Response.Write(""); } Response.Write("添加新学生信息"); 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" %>





    
    


    
添加学生信息
学号:

姓名:

性别:       

专业: 计算机科学与技术 物联网工程 电子科学与技术 电子通信 软件工程

手机:

 

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.删除" %>





    


    

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.修改" %>





    
    


    
学号:

姓名:

性别:     

专业: 计算机科学与技术 物联网工程 电子科学与技术 电子通信 软件工程

手机:

 

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);
        }
         
        
    }
}

结语

本次设计代码规整,完美实现增删改查操作,前端可以进行自定义调整。

你可能感兴趣的:(c#学习)