数据库专题训练实验-------数据库应用系统开发
【实验目的】
- 掌握数据库连接技术
【实验环境】
Sql server 2018
Visual Studio 2022
【实验内容】
- 需求分析
数据库是一种存储数据并对数据进行操作的工具。数据库的作用在于组织和表达信息,简而言之,数据库就是信息的集合。计算机的数据库可以分为两类:非关系数据库和关系数据库。关系数据库中包含了多个数据表的信息,数据库含有各个不同部分的术语,如记录、域等。
SQLserver 2018就是关系数据库开发工具,数据库能汇集各种信息以供查询、存储和检索。SQL 的优点在于它集数据查询、数据操纵、数据定义和数据控制功能于一体。
教务处的管理人员录入全校的课程基本信息和本学期的课程授课教师、地点、时间;
在学生入学的时候,学院的管理人员录入学生基本信息;
学生每学期自己上网登录系统选课,选课成功后信息存入数据库中,学生自己可以查询选课的情况;
学生选课不成功的情况有:
所选课程的先修课还没有记录,系统提示“缺先修课,选课失败”;
本学期所选课程的上课时间有冲突,系统提示“上课时间有冲突,选课失败”;
学生一学期所选课程的学分最多不能超18学分
学生可以注销所选课程。
学院管理员可以查询学生前几学期的选课信息、可以查询课程基本信息、学生基本信息;
当学生退学时,由教务处的管理人注销学生基本信息
如果开课之后,学生要求退课,则由教务处的工作人员为学生注销所选课程;
允许学生休学,教务处为休学的退学做学籍冻结处理;复学后为其办理解冻处理;
每学期教务处为学生办理学期注册手续;没有办理学期注册的学生不能选课;
学期末,学院工作人员负责录入学生的成绩。
2、用户设计
登录界面设计
教务处界面设计
学院界面设计
学生界面设计
学院登录后:
学生登录后界面:
【整体代码】
登录界面代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void butEnter_Click(object sender, EventArgs e)
{
string userkind = this.userkindcmb.Text;
string userid = this.textBox1.Text;
string userpwd = this.textBox2.Text;
string userpwddb;
string queryString = "select * from ADMINISTRATOR where ID = " + userid + " and DEPARTMENT = '" + userkind + "' and PASSWORD = '" + userpwd + "'";
//MessageBox.Show(queryString);
// SqlConnection connection = new SqlConnection("server=.;integrated security=true;database=XKXT");
SqlConnection connection = new SqlConnection("server=.;integrated security=true;database=XKXT");
//sqlexpress;
// queryString = "select PASSWORD from ADMINISTRATOR where NAME=" + userid;
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
adapter.Fill(ds);
userpwddb = ds.Tables[0].Rows[0][0].ToString();
if (userkind.Length == 0 || userid.Length == 0 || userpwd.Length == 0)
MessageBox.Show("输入下信息不完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
if (userpwddb == userpwd)
{
MessageBox.Show("成功登录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (userkind == "教务处")
{
frm_admin frmadmin = new frm_admin();
frmadmin.strUserid = userid;
frmadmin.Show();
}
if (userkind == "学院")
{
frm_school frmschool = new frm_school();
frmschool.strUserid = userid;
frmschool.Show();
} if (userkind == "学生")
{
frm_student frmstudent = new frm_student();
frmstudent.strUserid = userid;
frmstudent.Show();
}
//关闭登录界面
this.Visible = false;
textBox1.Text = "";
textBox2.Text = "";
userkindcmb.Text = "";
}
else
{
MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void butExit_Click(object sender, EventArgs e)
{
//退出
Application.Exit();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
教学教务管理代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class frm_admin : Form
{
public string strUserid;
public frm_admin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
//private void button2_Click(object sender, EventArgs e)
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
//密码修改
string userpwd = textBox1.Text;
string queryString = "select * from ADMINISTRATOR where ID = " + strUserid + " and DEPARTMENT = '教务处'";
SqlConnection connection = new SqlConnection("server=.;integrated security=true;database=XKXT");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter); //数据更新时用
adapter.SelectCommand = new SqlCommand(queryString, connection);
adapter.Fill(ds);
ds.Tables[0].Rows[0][1] = userpwd;
adapter.Update(ds);
MessageBox.Show("密码已经成功修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button3_Click(object sender, EventArgs e)
{
//增加一门课程记录的例子
SqlConnection connection = new SqlConnection("server=.;integrated security=true;database=XKXT");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter); //数据更新时用
int id = int.Parse(textBox2.Text);
string name = textBox3.Text;
string classroom = textBox4.Text;
string teacher = textBox5.Text;
string time = textBox6.Text;
int credit = int.Parse(textBox7.Text);
string note = textBox9.Text;
int precourse = 0;
if (textBox8.Text != "")
precourse = int.Parse(textBox8.Text);
string queryString = "select * from C where 1=0";
//MessageBox.Show(queryString);
adapter.SelectCommand = new SqlCommand(queryString, connection);
adapter.Fill(ds);
DataRow dr = ds.Tables[0].NewRow();
dr["ID"] = id;
dr["NAME"] = name;
dr["CLASSROOM"] = classroom;
dr["TEACHER"] = teacher;
dr["TIME"] = time;
dr["CREDIT"] = credit;
dr["NOTE"] = note;
if (precourse != 0)
dr["PRECOURSE"] = precourse;
ds.Tables[0].Rows.Add(dr);
adapter.Update(ds);
MessageBox.Show("成功添加", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
//删除某学生的选课记录的例子
SqlConnection connection = new SqlConnection("server=.;integrated security=true;database=XKXT");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter); //数据更新时用
string id = textBox11.Text;
string queryString = "select * from SC where SID = " + id;
adapter.SelectCommand = new SqlCommand(queryString, connection);
adapter.Fill(ds);
//MessageBox.Show(ds.Tables[0].Rows[0][0].ToString());
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
ds.Tables[0].Rows[i].Delete();
adapter.Update(ds);
MessageBox.Show("该学生选课记录已经删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button7_Click(object sender, EventArgs e)
{
}
}
}
学院代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class frm_school : Form
{
public string strUserid;
public frm_school()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_school_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“xKXTDataSet2.C”中。您可以根据需要移动或删除它。
this.cTableAdapter.Fill(this.xKXTDataSet2.C);
// TODO: 这行代码将数据加载到表“xKXTDataSet1.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.xKXTDataSet1.SC);
// TODO: 这行代码将数据加载到表“xKXTDataSet.S”中。您可以根据需要移动或删除它。
this.sTableAdapter.Fill(this.xKXTDataSet.S);
}
private void button2_Click(object sender, EventArgs e)
{
//密码修改
string userpwd = textBox1.Text;
string queryString = "select * from ADMINISTRATOR where ID = " + strUserid + " and DEPARTMENT = '教务处'";
SqlConnection connection = new SqlConnection("server=(local)\\sqlexpress;integrated security=true;database=XKXT");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter); //数据更新时用
adapter.SelectCommand = new SqlCommand(queryString, connection);
adapter.Fill(ds);
ds.Tables[0].Rows[0][1] = userpwd;
adapter.Update(ds);
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}
表值对应:
【功能举例演示】
1、打开登录界面,合理填充个人信息
如果有没填的报错,提示
if (userkind.Length == 0 || userid.Length == 0 || userpwd.Length == 0)
MessageBox.Show("输入下信息不完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
按照数据库信息正确登录:
成功登录:
在课程录入中正确填写信息:
返回数据库查看新添加的信息,刷新:
可以看到能够正常运行。
其他功能不在一一演示,代码部分列举。