信息化系统开发与应用
一.Visual Studio的实训课题
使用Visual Studio开发简单系统(以会议室管理系统为例子)
二.实验环境及配置介绍
开发环境:Linux/Windows
开发语言:C#
数据库:SQL server2010
系统架构:C/S
软件技术:Visual Studio
三.系统需求
角色:管理员、员工。
功能:
1)管理员:员工管理、会议室管理、会议室分类管理、审核会议预定、会议通知管理、会议资料管理、会议投票管理、查看员工考勤、通知公告管理、意见收集管理、设备报修回复。
2)员工:查看会议室信息、预定会议室、查看会议资料、会议投票、查看通知公告、考勤打卡、设备报修。
四.系统概要设计
主要技术:
C/S架构:C/S架构是一种比较早的软件架构,主要应用于局域网内。在这之前经历了集中计算模式,随着计算机网络的进步与发展,尤其是可视化工具的应用,出现过两层C/S和三层C/S架构,不过一直很流行也比较经典的是我们所要研究的两层C/S架构。
C/S架构软件(即客户机/服务器模式)分为客户机和服务器两层:第一层是在客户机系统上结合了表示与业务逻辑,第二层是通过网络结合了数据库服务器。简单的说就是第一层是用户表示层,第二层是数据库层。
数据库:SQL Server是一个可扩展的、高性能的、为分布式客户机/服务器计算所设计的数据库管理系统,SQL Server是一种关系型数据库管理系统,可以采用SQL语句来执行各种各样的操作,例如更新数据库中的数据,从数据库中提取数据等。
C#语言:C#是由C和C++衍生出来的一种安全的、稳定的、简单的、优雅的面向对象编程语言。C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程的支持成为.NET开发的首选语言。
会议室管理系统拥有两种用户,每种用户设计的具体功能操作如下图:
五.系统E-R图设计
根据相应的功能需求分析得出结果,设计出了“企业会议室管理系统”数据库E-R图,如下所示:
六.系统数据库设计
根据企业会议室管理的要求和实际情况进行分析,可以确定该数据库的结构,包括如下四张表格:
room_info会议室信息表:
属性列名 | 属性描述 | 数据类型 | 约束条件 |
---|---|---|---|
room_id | 会议室id | int | Primary key(PK) |
room_loca | 会议室位置 | varchar(20) | |
room_pnum | 会议室容量(人数) | int | |
room_meettype | 会议室预约情况 | varchar(20) | |
room_eq | 会议室设备情况 | varchar(50) |
SQL sever语句创建表
#使用sql语句首先先创建一个库(以下两行写完后先执行一下)
create database room_info;
use room_info;
#然后使用sql语句创建room_info会议室信息表
create table room_info(
room_id int primary KEY ,
room_loca varchar(20),
room_pnum int,
room_meettype varchar(20),
room_eq varchar(50)
);
attendant_info会议室管理人员表:
属性列名 | 属性描述 | 数据类型 | 约束条件 |
---|---|---|---|
attendant_id | 管理员id | int | Primary key(主键) |
attendant_name | 管理员名字 | varchar(20) | |
attendant_pnum | 管理员电话号 | varchar(20) | |
attendant_roomid | 管理的会议室id | int | Foreign key(外键)依赖于room_info表的room_id |
attenant_job | 主要工作内容 | varchar(20) |
#使用sql语句创建attendant_info会议室管理人员表
create table attendant_info(
attendant_id int primary key ,
attendant_name varchar(20),
attendant_pnum varchar(20),
attendant_roomid int,
attendant_job varchar(20),
constraint room_attendant foreign key (attendant_roomid) references room_info(room_id)
);
user_info用户信息表:
属性列名 | 属性描述 | 数据类型 | 约束条件 |
---|---|---|---|
user_id | 用户id | int | Primary key(PK) |
user_name | 用户名字 | varchar(20) | |
user_pnum | 用户电话号 | varchar(20) |
#使用sql语句创建user_info用户信息表
create table user_info(
user_id int(6) primary key ,
user_name varchar(20),
user_pnum varchar(20)
);
meet_info预约信息表:
属性列名 | 属性描述 | 数据类型 | 约束条件 |
---|---|---|---|
meet_id | 预约id | int(10) | Primary key(主键) |
meet_rid | 预约的会议室号 | int | Foreign key(外键)依赖于room_info表的room_id |
meet_uid | 预约的用户id | int | Foreign key(外键)依赖于user_info表的user_id |
meet_time | 用户预约的时间段 | varchar(20) |
#使用sql语句创建meet_info预约信息表
create table meet_info(
meet_id int(10) primary key,
meet_rid int(4),
meet_uid int(6),
meet_time varchar(20),
constraint room_meet foreign key (meet_rid) references room_info(room_id),
constraint user_meet foreign key (meet_uid) references user_info(user_id)
);
数据库各表之间的关系图:
建立好数据库后可以在数据库中添加一些数据方便后面增删改查
(需要注意的是添加数据的时候设置有外键约束的字段需要根据你的依赖关系进行数据添加)
创建好数据库后需要通过Visual Studio来连接SQL server
七.系功能实现
使用Visual Studio的代码部分
1.系统登录界面
通过c#语句写出以上的功能实现
namespace new_WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.skinEngine1.SkinFile = "MSN.ssk";
}
private void button1_Click(object sender, EventArgs e)
{
//用户名:123456 密码123456
string nema = this.textBox1.Text;
string paw= this.textBox2.Text;
if(nema=="123456")
{
//用户名正确
if(paw=="123456")
{
this.Hide();
加载界面 ld = new 加载界面();
ld.Show();
}
else
{
MessageBox.Show("密码不正确!");
}
}
else
{
//用户名不正确
MessageBox.Show("用户名不正确!");
}
}
2.创建一个类来连接SQL server
以下SQL server 与Visual Studio在一台主机上,也可以使用非同一台主机(代码稍有一点点区别)
public class DBHelper //#DBHelper为类的一个名字
{
//连接字符串
//xxxxxx\SQL为sql server服务器名称直接在sql server去查看即可
//安装sql server时如没有更改sa为默认的
//pwd为登录sql server时的密码
//room_info为你创建数据库时给库取的名字
string conStr = @"server=xxxxxxx\SQL;uid=sa;pwd=123456;database=room_info";
//完成一个查询数据的的方法
public DataTable getData(string sql)
{
//创建连接
SqlConnection con = new SqlConnection(conStr);
//打开链接
con.Open();
//创建一个适配器
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
//载入
sda.Fill(dt);
return dt;
}
/// <summary>
/// 完成添加,修改,删除,的功能
/// </summary>
/// <param name="sql">参数是SQL命令</param>
/// <returns>返回值为受影响的行数</returns>
public int InUpDe(string sql)
{
int i = 0;
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
i=cmd.ExecuteNonQuery();
return i;
}
}
创建好连接数据库的类 ‘DBHelper’ 后每次当要增删改查时,都要调用此类中的方法(后面的代码中都有实现)
3.系统主界面
namespace new_WindowsFormsApp1
{
public partial class 主界面 : Form
{
public 主界面()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
attendant跳转 f = new attendant跳转();
f.Show();
}
private void button2_Click(object sender, EventArgs e)
{
meet跳转 f = new meet跳转();
f.Show();
}
private void button3_Click(object sender, EventArgs e)
{
room跳转 f = new room跳转();
f.Show();
}
private void button4_Click(object sender, EventArgs e)
{
user跳转 f = new user跳转();
f.Show();
}
private void 主界面_Load(object sender, EventArgs e)
{
}
}
}
4.attendant跳转界面(以attendant为例后面三个表都是如此)
当点击 ‘attendant’ 按钮时会跳转出以下界面
namespace new_WindowsFormsApp1
{
public partial class attendant跳转 : Form
{
public attendant跳转()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
attendant f = new attendant();
f.Show();
}
private void button2_Click(object sender, EventArgs e)
{
attendant添加 f = new attendant添加();
f.Show();
}
private void attendant跳转_Load(object sender, EventArgs e)
{
}
}
}
4.1.点击 ‘查询’ 按钮时出现以下界面
(表中数据需要在SQL serverer软件中提前添加,如不添加查询不到任何的数据)
以上查询块并是只有进行查询,还可以对表进行修改与删除操作(在以下代码中都有实现)
(需要注意的是某些表的字段被条件约束为外键约束,并不允许随便更改,需要根据你的依赖关系进行修改)
namespace new_WindowsFormsApp1
{
public partial class attendant : Form
{
public attendant()
{
InitializeComponent();
}
private void attendant_Load(object sender, EventArgs e)
{
string sql = "select * from attendant_info";
DBHelper db = new DBHelper();
this.dataGridView1.DataSource = db.getData(sql);
this.textBox2.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
this.textBox3.Text = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
this.textBox4.Text = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
this.textBox5.Text = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
this.textBox6.Text = this.dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "select * from attendant_info where attendant_id like'%"+this.textBox1.Text+"%'";
DBHelper db = new DBHelper();
this.dataGridView1 .DataSource = db.getData(sql);
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.textBox2.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
this.textBox3.Text = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
this.textBox4.Text = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
this.textBox5.Text = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
this.textBox6.Text = this.dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
string id = this.textBox2.Text;
string name = this.textBox3.Text;
string pnum = this.textBox4.Text;
string roomid = this.textBox5.Text;
string job = this.textBox6.Text;
string sql = "update attendant_info set attendant_name='"+name+"',attendant_pnum='"+pnum+"',attendant_roomid='"+roomid+"',attendant_job='"+job+ "' where attendant_id ="+id;
DBHelper dbh = new DBHelper();
int a = dbh.InUpDe(sql);
if (a > 0)
{
MessageBox.Show("数据修改成功!");
string sql2 = "select * from attendant_info";
this.dataGridView1.DataSource = dbh.getData(sql2);
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string id = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string sql = "delete from attendant_info where attendant_id =" + id;
//确认操作
DialogResult dr = MessageBox.Show("确认要删除吗", "确认", MessageBoxButtons.YesNo);
DBHelper dbh = new DBHelper();
int a = dbh.InUpDe(sql);
if (a > 0)
{
MessageBox.Show("数据删除成功!");
string sql2 = "select * from attendant_info";
this.dataGridView1.DataSource = dbh.getData(sql2);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
4.2.点击 ‘添加’ 按钮时出现以下界面
namespace new_WindowsFormsApp1
{
public partial class attendant添加 : Form
{
public attendant添加()
{
InitializeComponent();
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string id = this.textBox1.Text;
string name = this.textBox2.Text;
string pnum = this.textBox3.Text;
string roomid = this.textBox4.Text;
string job = this.textBox5.Text;
string sql = "insert into attendant_info values('" + id + "','" + name + "','" + pnum + "','" + roomid + "','" + job + "')";
DBHelper dbh = new DBHelper();
int a = dbh.InUpDe(sql);
if(a>0)
{
MessageBox.Show("数据添加成功!");
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
其他三个模块都与此模块大同小异,各模块的功能相对简单单一。