请使用Web服务器控件:TextBox控件、Button按钮控件和GridView控件设计一个学生信息管理系统。功能包括学生信息的增、删、改、查功能。数据库采用SQL Server 2008,使用Windows身份验证模式。建立student数据库,并建立studinfo数据表,表中字段有:学号(字符型,10,必填字段),姓名(字符型,8),性别(字符型,2)和年龄(整型,2)。学生信息添加页面为add.aspx,查询页面为find.aspx,修改和删除放在一个页面中完成,取名为editdel.aspx。
工具:vs2017 ,SQLserver2008
1.添加页面add.aspx
设计界面
代码:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
namespace Demo5
{
public partial class add : System.Web.UI.Page
{
//数据连接最基本需要的对象
private SqlConnection con = null;
private SqlCommand cmd = null;
//全局对象的sql语句
private string sql = null;
//打开数据库
public void openDatabase()
{
con = new SqlConnection();
con.ConnectionString = "Integrated Security=SSPI;Data Source=DESKTOP-MRMMLS8;initial catalog=studb";
if(con.State==ConnectionState.Closed)
{
con.Open();
}
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataBind();
}
//封装的数据库语句执行的方法
public void execute(string sql)
{
openDatabase();
cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
sql = "insert into studinfo(学号,姓名,性别,年龄) values('" + TextBox1.Text.ToString().Trim() + "','" + TextBox2.Text.ToString().Trim() + "','" + TextBox3.Text.ToString().Trim() + "','" + TextBox4.Text.ToString().Trim() + "')";
execute(sql);
GridView1.DataBind(); //重新加载表
}
protected void GridViewBind( )
{
string sql = "select * from studinfo";// order by id 学号"; //定义执行查询操作的SQL语句
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd); //实例化SqlDataAdapter对象
DataSet ds = new DataSet(); //实例化数据集DataSet
da.Fill(ds,"学号");
//GridView1.DataSource = ds;
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind(); //绑定数据库表中数据
/*
* 连接时gridview处先连接数据库,直接能连接上表
* 不可以直接连上表
*
*
*/
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("find.aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("editdel.aspx");
}
}
}
2.查询页面find.aspx
设计界面:
代码为:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo5
{
public partial class find : System.Web.UI.Page
{
//数据连接最基本需要的对象
private SqlConnection con = null;
private SqlCommand cmd = null;
//全局对象的sql语句
private string sql = null;
string no;
//打开数据库
public void openDatabase()
{
con = new SqlConnection();
con.ConnectionString = "Integrated Security=SSPI;Data Source=DESKTOP-MRMMLS8;initial catalog=studb";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
public void load(String sql)
{
openDatabase();
cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox2.Text = (String)dr[1].ToString().Trim();
TextBox3.Text = (String)dr[2].ToString().Trim();
TextBox4.Text = (String)dr[3].ToString().Trim();
}
con.Close();
}
//封装的数据库语句执行的方法
public void execute(string sql)
{
openDatabase();
cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataBind();
}
protected void Button4_Click(object sender, EventArgs e)
{
sql = "select * from studinfo where 学号='" + TextBox1.Text.ToString().Trim() + "'";
//TextBox1.Text="sele"
load(sql);
GridView1.DataBind();
}
protected void Button5_Click(object sender, EventArgs e)
{
Response.Redirect("add.aspx");
}
}
}
3. 修改和删除页面editdel.aspx
设计界面:
代码为:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo5
{
public partial class editdel : System.Web.UI.Page
{
//数据连接最基本需要的对象
private SqlConnection con = null;
private SqlCommand cmd = null;
//全局对象的sql语句
private string sql = null;
// string no;
//打开数据库
public void openDatabase()
{
con = new SqlConnection();
con.ConnectionString = "Integrated Security=SSPI;Data Source=DESKTOP-MRMMLS8;initial catalog=studb";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
//默认加载页面的方法 找到年龄最大的加载
//有些问题,年龄不能相同,加载中前台的textbox里只能显示一条记录,数据拿到之后有多条只显示一条
public void load()
{
openDatabase();
string mmmm = TextBox1.Text.ToString().Trim();
cmd = new SqlCommand("select * from studinfo where 学号='mmmm' ", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox2.Text = (String)dr[1].ToString().Trim();
TextBox3.Text = (String)dr[2].ToString().Trim();
TextBox4.Text = (String)dr[3].ToString().Trim();
}
con.Close();
}
public void load(String sql)
{
openDatabase();
cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox2.Text = (String)dr[1].ToString().Trim();
TextBox3.Text = (String)dr[2].ToString().Trim();
TextBox4.Text = (String)dr[3].ToString().Trim();
}
con.Close();
}
//封装的数据库语句执行的方法
public void execute(string sql)
{
openDatabase();
cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
//删除
sql = "delete from studinfo where 学号='" + TextBox1.Text.ToString().Trim() + "' ";
execute(sql);
load();
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
//修改
sql = "update studinfo set 姓名='"+ TextBox2.Text.ToString().Trim()+ "', 性别='" + TextBox3.Text.ToString().Trim() + "' , 年龄 ='"+ TextBox4.Text.ToString().Trim()+ "' where 学号='" + TextBox1.Text.ToString().Trim() + "'";
execute(sql);
GridView1.DataBind();
}
protected void Button4_Click(object sender, EventArgs e)
{
Response.Redirect("add.aspx");
}
}
}
4.运行视图为:
添加后:
可以转到find界面,根据学号进行查询,并且显示在textbox里面:
点击返回之后转到添加界面,再转到editdel界面,进行修改和删除:
删除:
修改:
5.数据库代码为:
USE [studb]
GO
/****** Object: Table [dbo].[studinfo] Script Date: 04/20/2020 00:29:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[studinfo](
[学号] [nchar](10) NOT NULL,
[姓名] [nchar](8) NULL,
[性别] [nchar](2) NULL,
[年龄] [int] NULL,
CONSTRAINT [PK_studinfo] PRIMARY KEY CLUSTERED
(
[学号] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
6.注意点:
(1).vs连接数据库时,在服务器资源管理器里
数据连接 右击 添加连接
服务器名通常为(local),英文括号,输入数据库名称即可。
(2).gridview可以直接连接数据库,如下图:在选择数据源中连接数据库,不是直接连接文件!!