dynamicweb8-13webform1.aspx
webform1.aspx.cs<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dynamicweb8_13.WebForm1" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>查询学生信息</h4>
学号:<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="查询" OnClick="btnSearch_Click" />
<asp:RequiredFieldValidator ID="valrID" runat="server" ErrorMessage="输入学号" ControlToValidate="txtID" Display="Dynamic" ForeColor="#FF3300" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="valgID" runat="server" ErrorMessage="学号无效,未在范围内" ControlToValidate="txtID" ForeColor="#FF3300" MaximumValue="20509999" MinimumValue="20000001" SetFocusOnError="True" Type="Integer"></asp:RangeValidator>
<br />
<asp:Label ID="lblTips" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
web.configusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace dynamicweb8_13
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string cnString = System.Configuration.ConfigurationManager.ConnectionStrings["cnStudent"].ToString();
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(cnString);
con.Open();
string sql = "select * from student where stu_id=@stu_id";
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@stu_id", txtID.Text);
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
lblTips.Text = "";
lblTips.Text += "<br><br>";
lblTips.Text += "学号:" + reader.GetString(0) + "<br>";
lblTips.Text += "姓名:" + reader.GetString(1) + "<br>";
lblTips.Text += "性别:" + reader.GetString(2) + "<br>";
lblTips.Text += "生日:" + reader.GetDateTime(3) + "<br>";
lblTips.Text += "专业:" + reader.GetString(4) + "<br>";
lblTips.Text += "是否党员:" + reader.GetBoolean(5) + "<br>";
lblTips.Text += "电子邮件:" + reader.GetString(6) + "<br>";
}
}
else
{
lblTips.Text = "该学号的学生信息不存在";
}
reader.Close();
con.Close();
}
}
}
sql<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="cnStudent" connectionString="Data Source=LAPTOP-AQKEN65V\SQLEXPRESS08;Initial Catalog=studentMIS;Persist Security Info=True;User ID=sa;Password=123456"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
--学生管理系统库
create database studentMIS;
use studentMIS;
--学生表
create table student(
stu_id nvarchar(8) not null,
stu_name nvarchar(8) not null,
sex nvarchar(2) not null,
birth_date datetime,
major nvarchar(50) not null,
isCPC bit,
email nvarchar(20),
primary key(stu_id)
);
--课程表
create table course(
course_id smallint identity(1,1) not null,
course_name nvarchar(50) not null
);
--成绩表
create table result(
stu_id nvarchar(8) not null,
course_id smallint not null,
mark decimal(6,0) not null,
primary key(stu_id,course_id)
);
insert into student values('20160101','王霞','女','1998-10-23','计算机信息管理','true','wx@163.com');
insert into student values('20160102','张大力','男','1998-07-02','计算机信息管理','false','zdf@163.com');
insert into student values('20160103','李长军','男','1998-5-14','计算机信息管理','false','lcj@163.com');
insert into student values('20160104','韩华东','男','1998-12-07','计算机信息管理','false','8562478@qq.com');
insert into student values('20160105','万芳','女','1998-01-27','软件技术','false','5478518@qq.com');
insert into student values('20160106','赵中千','男','1998-08-12','软件技术','true','5582178@qq.com');
insert into student values('20160107','吴空俊','男','1998-05-21','软件技术','false','4845512@qq.com');
insert into student values('20160108','方文其','男','1998-06-05','软件技术','false','8412563@qq.com');
insert into student values('20160109','唐汉','男','1998-04-18','计算机应用','false','1546315@qq.com');
insert into student values('20160110','刘含斌','男','1998-10-07','计算机应用','false','741526@qq.com');
insert into course values('高等数学');
insert into course values('大学英语');
insert into course values('邓小平理论概述');
insert into course values('毛泽东思想概述');
insert into course values('C语言程序设计');
insert into course values('数据结构');
insert into result values('20160101',1,'71');
insert into result values('20160101',2,'72');
insert into result values('20160101',3,'73');
insert into result values('20160101',4,'74');
insert into result values('20160101',5,'75');
insert into result values('20160101',6,'76');
insert into result values('20160102',1,'60');
insert into result values('20160102',2,'61');
insert into result values('20160102',3,'60');
insert into result values('20160102',4,'62');
insert into result values('20160102',5,'60');
insert into result values('20160102',6,'59');