资源描述
《C#程序设计》大作业
题 目: 设备管理系统
专 业: 计算机科学与技术s
学 号: 121096143
姓 名: 朱 晓 敏
完成日期: 2012/11/6
目 录
1 前言 2
2 需求分析 2
2.1要求 2
2.2任务 2
2.3运行环境 2
2.4开发工具 2
3 概要设计与详细设计 3
3.1系统流程图 3
3.2数据库设计 4
3.2.1建立数据字典 4
3.2.2数据库详细设计 4
4 编码与实现 5
4.1分析 5
4.2具体代码实现 7
4.3界面实现 16
5 课程设计总结 24
参考文献 25
评语及成绩 0
1 前言
设计一个设备管理系统,该系统主要针对设备管理员。系统首先要求用户登录,用户必须输入正确的用户名和密码;系统主界面包括设备查询功能及数据维护功能,设备查询功能是按一定的条件查询所需要的设备信息,数据维护主要是通过增加或删除来修改数据。
2 需求分析
2.1要求
(1)用Csharp语言实现程序设计;
(2)采用.NET开发工具来设计主窗体和子窗体等;
(3)画出系统模块的流程图;
(4)完成数据库的设计;
(5)界面友好(良好的人机互交),程序要有注释。
2.2任务
(1)设计一个登陆窗体和主窗体,7个子窗体来显示相关信息;
(2)管理员必须输入正确的用户名和密码,才能进入主窗体进行相关操作;
(3)画出所有模块的流程图;
(4)完成数据库的设计;
(5)编写代码;
(6)程序分析与调试。
2.3运行环境
(1)WINDOWS2000/XP系统
(2)Visual Studio 2005编译环境
2.4开发工具
C#: C#(C Sharp)是微软为NET Framework量身订做的程序语言,C#拥有C/C++的强大功能以及Visual Basic简易使用的特性,是第一个组件导向(Component-oriented)的程序语言,和C++与Java一样亦为对象导向(object-oriented)程序语言。
3 概要设计与详细设计
3.1系统流程图
首先要有一个登录模块对登录用户进行验证,如果验证成功则进入系统的主窗体,登录主窗体之后管理员以操作所有的功能:查询、修改、增加设备信息、辅助工具、退出。
开始
用户名及密码
选择操作类型
查询设备信息
修改设备信息
添加设备信息
删除设备信息
设备信息表
退出
N
Y
图3.1 系统流程图
3.2数据库设计
3.2.1建立数据字典
在开发设备管理系统之前,分析了改系统的数据量。选择Microsoft SQL Server2005数据库存储这些信息,数据库命名为MyDevice,在数据库中创建了2个数据表用于不同的信息。
1.设备管理员数据字典
名字:设备管理员表(User)
描述:记录管理员的具体详细信息
定义:设备管理员表=用户编号+用户名+密码
位置:设备管理数据库
2.设备数据字典
名字:设备信息表(equipment)
描述:记录设备的具体详细信息
定义:设备信息表=设备编号+设备名称+设备数量+设备价格
位置:设备管理数据库
3.2.2数据库详细设计
表1 User表结构
列名
数据类型
说明
userId
int
用户编号,主键,标识列,表示增量1,标识种子1
UserName
nvarchar(50)
用户名,非空
password
nvarchar(50)
密码,非空
表2 equipment表结构
列名
数据类型
说明
id
int
设备编号,主键,标识列,表示增量1,标识种子1
name
nvarchar(50)
设备名称,非空
price
money
设备名称,非空
count
int
设备数量,非空
4 编码与实现
4.1分析
(1)登陆界面的设计——打开Visual Studio 2005,新建一个名为DeviceSystem项目,然后打开一个窗体并命名为userlogin.cs。在此窗体中添加2个标签(用户名和密码)、2个按钮(确定和取消)和2个textBox等,如图所示
图4.1 登陆窗体
(2)系统主窗体的设计——添加窗体并命名为frmMain.cs,在此窗体添加一个MenuStrip控件,一个 ToolStrip控件及3个按钮,一个Time控件,toolStripStatus控件并分别设置各属性,如图所示
图4.2 系统主窗体
(3)子窗体的设计——添加4个窗体并依此命名为frmselecName.cs、frmselecPrice.cs、frmDataMaint.cs、frmDeletedevice.cs并分别添加工具控件及设置各属性,如图所示
图4.3 按名称查询窗
图4.4 按价格查询窗体
图4.5 数据维护窗体
4.2具体代码实现
1.登录窗体—frmlogin.cs
using System;
using System.Configuration;
……………………
namespace DeviceSystem
{…………………………
private void btnYes_Click(object sender, EventArgs e)
{
string userName = txtName.Text;
string password = txtPwd.Text;
string cons = ConfigurationManager.ConnectionStrings["DeviceSystem.Properties.Settings.MyDeviceConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(cons);
//获取用户名和密码匹配的行的数量的SQL语句 string sql=String.Format("select count(*) from [User] where username='{0}'and password='{1}'",userName,password);
try{
connection.Open(); //打开数据库连接
SqlCommand command = new SqlCommand(sql,connection);
//创建Command对象
int num = (int)command.ExecuteScalar();
//执行查询语句,返回匹配的行数
if (num > 0)
{ //如果有匹配的行,则表明用户名和密码正确
MessageBox.Show("欢迎进入设备管理系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
frmMain mainForm=new frmMain(); //创建主窗体对象
mainForm.Show(); //显示窗体
this.Visible=false; //登陆窗体隐藏
}
else {
MessageBox.Show("您输入的用户名或密码错误!", "登录失败", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation);
}//MessageBoxIcon.Exclamation是由三角符号组成的警惕图
}
catch (Exception ex){
MessageBox.Show(ex.Message,"操作数据库出错啦!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
finally{
connection.Close(); //关闭数据库连接
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtPwd.Text = "";
txtName.Focus();//将光标指定在txtName上
}
}
}
2.主窗体frmMain.cs
using System;
using System.Windows.Forms;
……………………
namespace DeviceSystem
{…………………………
private void timer1_Tick(object sender, EventArgs e)
{
DateTime dt = DateTime.Now; //获取当前时间
tssData.Text = dt.ToLongDateString() ;
}
private void tsmExit_Click(object sender, EventArgs e)
{ Application.Exit();
}
private void tsmSelecName_Click(object sender, EventArgs e)
{
frmselecName selectname = new frmselecName(); //创建子窗体对象
selectname.MdiParent = this; //指定当前窗体为MDI父窗体
selectname.Show(); //打开子窗体
tssStatus.Text = "按名称查询"; //在状态栏中显示操作内容
}
private void tsmSelecPrice_Click(object sender, EventArgs e)
{
frmselecPrice selectprice = new frmselecPrice(); //创建子窗体对象
selectprice.MdiParent = this; //指定当前窗体为MDI父窗体
selectprice.Show(); //打开子窗体
tssStatus.Text = "按单价查询"; //在状态栏中显示操作内容
}
private void tsmUpdate_Click(object sender, EventArgs e)
{
frmDataMaint datamaint = new frmDataMaint(); //创建子窗体对象
datamaint.MdiParent = this; //指定当前窗体为MDI父窗体
datamaint.Show(); //打开子窗体
tssStatus.Text = "修改数据"; //在状态栏中显示操作内容
}
private void tsmabout_Click(object sender, EventArgs e)
{
frmAbout about = new frmAbout(); //创建子窗体对象
about.MdiParent = this; / /指定当前窗体为MDI父窗体
about.Show(); //打开子窗体
tssStatus.Text = "关于我们"; //在状态栏中显示操作内容
}
private void tsmjsq_Click(object sender, EventArgs e)
{
frmjsq jsq = new frmjsq(); //创建子窗体对象
jsq.MdiParent = this; //指定当前窗体为MDI父窗体
jsq.Show(); //打开子窗体
tssStatus.Text = "计算器"; //在状态栏中显示操作内容
}
private void tsmdate_Click(object sender, EventArgs e)
{
frmTime time = new frmTime(); //创建子窗体对象
time.MdiParent = this; //指定当前窗体为MDI父窗体
time.Show(); / /打开子窗体
tssStatus.Text = "万年历"; //在状态栏中显示操作内容
}
private void tsmdel_Click(object sender, EventArgs e)
{
frmDeletedevice delete = new frmDeletedevice(); //创建子窗体对象
delete.MdiParent = this; //指定当前窗体为MDI父窗体
delete.Show(); //打开子窗体
tssStatus.Text = "设备数据维护"; //在状态栏中显示操作内容
}
}
}
3.子窗体frmMain.cs
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
……………………
namespace DeviceSystem
{ ……………………
public frmselecName()
{
InitializeComponent();
string cons = ConfigurationManager.ConnectionStrings["DeviceSystem.Properties.Settings.MyDeviceConnectionString"].ConnectionString
connection = new SqlConnection(cons);
}
private void frmselecName_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“myDeviceDataSet.equipment”中。您可以根据需要移动或移除它。
this.equipmentTableAdapter.Fill(this.myDeviceDataSet.equipment);
}
private void btnSelectName_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
//按名称查询设备
string sql = String.Format("select * from equipment where name like '%{0}%'",name);
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet datSet = new DataSet("equipment");
dataAdapter.Fill(datSet);
//设置各列的显示数据字段
dataGridView1.Columns[0].DataPropertyName = "id";
dataGridView1.Columns[1].DataPropertyName = "name";
dataGridView1.Columns[2].DataPropertyName = "price";
dataGridView1.Columns[3].DataPropertyName = "count";
dataGridView1.DataSource = datSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
4.子窗体frmselecPrice.cs
using System;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
……………………
namespace DeviceSystem
{……………………………
public frmselecPrice()
{
InitializeComponent();
string cons = ConfigurationManager.ConnectionStrings["DeviceSystem.Properties.Settings.MyDeviceConnectionString"].ConnectionString;
connection = new SqlConnection(cons);
}
private void btnselectPrice_Click(object sender, EventArgs e)
{
// decimal表示十进制数
decimal price1, price2;
try
{
price1 = Convert.ToDecimal(textBox1.Text);
price2 = Convert.ToDecimal(textBox2.Text);
}
catch
{
price1 = 0;
price2 = 1000000M;//默认为最大值
}
if(price1>price2)
{//如果price1>price2,交换两者
decimal temp=price1;
price1=price2;
price2=temp;
}
//按价格查询设备
string sql=String.Format("select * from equipment where price between {0} and {1}",price1,price2);
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet datSet = new DataSet("equipment");
dataAdapter.Fill(datSet);
//设置各列的显示数据字段
dataGridView1.Columns[0].DataPropertyName = "id";
dataGridView1.Columns[1].DataPropertyName = "name";
dataGridView1.Columns[2].DataPropertyName = "price";
dataGridView1.Columns[3].DataPropertyName = "count";
dataGridView1.DataSource = datSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void frmselecPrice_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“myDeviceDataSet.equipment”中。您可以根据需要移动或移除它。
this.equipmentTableAdapter.Fill(this.myDeviceDataSet.equipment);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
5.子窗体frmDataMaint.cs
using System;
using System.Data.SqlClient;
……………………
namespace DeviceSystem
{……………………………
private void frmDataMaint_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“myDeviceDataSet.equipment”中。您可以根据需要移动或移除它。
this.equipmentTableAdapter.Fill(this.myDeviceDataSet.equipment);
}
private void btnsave_Click(object sender, EventArgs e)
{
equipmentTableAdapter.Update(myDeviceDataSet.equipment);
}
private void btnguanbi_Click(object sender, EventArgs e)
{ this.Close();
}
private void btnrefresh_Click(object sender, EventArgs e)
{
equipmentTableAdapter.Fill(myDeviceDataSet.equipment);
}
}
}
6.子窗体frmDeletedevice.cs
using System;
using System.Data.SqlClient;
using System.Configuration;
……………………
namespace DeviceSystem
{……………………
public partial class frmDeletedevice : Form
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
SqlCommand com;
public frmDeletedevice()
{
InitializeComponent();
}
private void frmDeletedevice_Load(object sender, EventArgs e)
{
BKY();
string cons = ConfigurationManager.ConnectionStrings["DeviceSystem.Properties.Settings.MyDeviceConnectionString"].ConnectionString;
con= new SqlConnection(cons);
////绑定cbosm
da = new SqlDataAdapter("select name from equipment", con);
ds = new DataSet();
da.Fill(ds, "equipment");
cbosm.DataSource = ds.Tables["equipment"];
cbosm.DisplayMember = "name";
FillDgvshebei();
}
private void FillDgvshebei()
{
//绑定dgvshebei
da = new SqlDataAdapter("select * from equipment", con);
ds = new DataSet();
da.Fill(ds, "equipment");
dgvshebei.DataSource = ds.Tables[0];
}
private void dgvshebei_CellClick(object sender, DataGridViewCellEventArgs e)
{
txthao.Text = Convert.ToString(dgvshebei["id", dgvshebei.CurrentCell.RowIndex].Value);
txtname.Text = Convert.ToString(dgvshebei["name", dgvshebei.CurrentCell.RowIndex].Value);
txtC.Text = Convert.ToString(dgvshebei["price", dgvshebei.CurrentCell.RowIndex].Value);
txtD.Text = Convert.ToString(dgvshebei["count", dgvshebei.CurrentCell.RowIndex].Value);
}
private void tsbdel_Click(object sender, EventArgs e)
{
com = new SqlCommand("delete from equipment where id='" + txthao.Text + "'", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
int i = (int)com.ExecuteNonQuery();
con.Close();
if (i > 0)
{
FillDgvshebei();
MessageBox.Show("删除成功!");
}
}
private bool bselect()
{
com = new SqlCommand("select count(*) from equipment where id='" + txthao.Text + "' and name='" + txtname.Text + "'", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
int i = (int)com.ExecuteScalar();
con.Close();
if (i > 0)
{
MessageBox.Show("已有这条记录!");
Clear();
return true;
}
else
{
return false;
}
}
private void Clear()
{
txthao.Text = "";
txtname.Clear();
}
private void tslbaocun_Click(object sender, EventArgs e)
{
if (txtname.Text == "" || txtC.Text == "" || tx
展开阅读全文