1、问题描述:
对职工信息进行管理,包括:职工信息的录入、显示、查询、修改、删除,要求有用户登录等windows窗口,用户数据存在数据库里。要求有用户登录等windows窗口,用户数据存在数据库里,并体现面向对象编程思想。
2、功能要求:
(1)录入职工信息并保存(职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等,并且要求职工号不重复;)
(2)显示所有职工信息
(3)查询职工信息(可按工号、按学历、按职务查询)
(4)修改职工信息并保存
(5)删除职工信息
(6)退出系统
值得一提的:
在c#中使用存储过程的写法
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlcmd.Connection = conn;
sqlcmd.CommandText = "xxxg";
sqlcmd.Parameters.Add("@职工号", SqlDbType.VarChar, 10).Value = textBox2.Text;
在c#中使用Bindingsource已经dataset的用法,dataGridview的列名设置。
bs = new BindingSource();
SqlDataAdapter dr = new SqlDataAdapter(sql2, conn);
DataSet myds = new DataSet();
dr.Fill(myds);
bs.DataSource = myds.Tables[0];//第一次写入默认为Tables[0],第二次为Tables[1]
this.dataGridView1.DataSource = bs;
dataGridView1.Columns[0].HeaderCell.Value = "职工号";//列名设置
dataGridView1.Columns[1].HeaderCell.Value = "名字";
dataGridView1.Columns[2].HeaderCell.Value = "性别";
获取选择行的数据(要在datagridview中设置选中为一行的设置)
int hs = dataGridView1.CurrentRow.Index;//获取选中的行数;
textBox2.Text = dataGridView1.Rows[hs].Cells[0].Value.ToString();//将获得的数据写在文本框上
存储过程的加入
写法
create proc name (
@职工号 varchar(10),
@姓名 varchar(10),
@性别 varchar(2),
@出生年月 varchar(20),
@学历 varchar(10),
@职务 varchar(15),
@工资 int,
@住址 varchar(10),
@电话 varchar(11)
)
as
insert into 职工信息汇总表 (职工号,姓名,性别,出生年月,学历,职务,工资,住址,电话)
values (@职工号,@姓名,@性别,@出生年月,@学历,@职务,@工资,@住址,@电话)
//as后面是真正起作用的句子,但要在前面定义要用到的变量要记得加@。
源码:
主程序
namespace 职工管理系统
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form2());
}
}
}
登录界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
namespace 职工管理系统
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string yh, connstr, mm1 = "";
yh = textBox1.Text;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
MessageBox.Show("数据库连接成功", "好");
string sql = "select 密码 from 用户账号表 where 用户名='" + yh + "'";
SqlCommand sqlcmd = new SqlCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataReader sqldr = sqlcmd.ExecuteReader();
if (sqldr.HasRows)//判断数据是不是空
{
while (sqldr.Read())//下一行
{
mm1 = sqldr["密码"].ToString();
}
}
if (mm1 == textBox2.Text && mm1 != "")
{
MessageBox.Show("登录成功", "登录验证", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
Form1 a = new Form1();
this.Hide();
a.Show();
}
}
}
}
管理界面:
using System.Data;
using System.Data.SqlClient;
namespace 职工管理系统
{
public partial class Form1 : Form
{
BindingSource bs;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 信息信息修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlcmd.Connection = conn;
sqlcmd.CommandText = "xxxg";
sqlcmd.Parameters.Add("@职工号", SqlDbType.VarChar, 10).Value = textBox2.Text;
sqlcmd.Parameters.Add("@姓名", SqlDbType.VarChar, 10).Value = textBox1.Text;
sqlcmd.Parameters.Add("@性别", SqlDbType.VarChar, 2).Value = textBox8.Text;
sqlcmd.Parameters.Add("@出生年月", SqlDbType.VarChar, 20).Value = textBox7.Text;
sqlcmd.Parameters.Add("@职务", SqlDbType.VarChar, 15).Value = textBox6.Text;
sqlcmd.Parameters.Add("@学历", SqlDbType.VarChar, 10).Value = textBox9.Text;
sqlcmd.Parameters.Add("@工资", SqlDbType.Int).Value = Convert.ToInt32(textBox3.Text);
sqlcmd.Parameters.Add("@住址", SqlDbType.VarChar, 10).Value = textBox5.Text;
sqlcmd.Parameters.Add("@电话", SqlDbType.VarChar, 11).Value = textBox4.Text;
if (sqlcmd.ExecuteNonQuery() >= 1)
{
MessageBox.Show("职工信息修改成功", "好");
}
conn.Close();
}
private void 信息删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr, sql,sql2,zgh;
zgh = textBox2.Text;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "delete from 职工信息汇总表 where 职工号='"+zgh+"'";
sql2 = "select * from 职工信息汇总表";
SqlCommand sqlcmd = new SqlCommand(sql,conn);
sqlcmd.ExecuteNonQuery();
bs = new BindingSource();
SqlDataAdapter dr = new SqlDataAdapter(sql2, conn);
DataSet myds = new DataSet();
dr.Fill(myds);
bs.DataSource = myds.Tables[0];
this.dataGridView1.DataSource = bs;
dataGridView1.Columns[0].HeaderCell.Value = "职工号";
dataGridView1.Columns[1].HeaderCell.Value = "名字";
dataGridView1.Columns[2].HeaderCell.Value = "性别";
dataGridView1.Columns[3].HeaderCell.Value = "出生日期";
dataGridView1.Columns[4].HeaderCell.Value = "学历";
dataGridView1.Columns[5].HeaderCell.Value = "职务";
dataGridView1.Columns[6].HeaderCell.Value = "工资";
dataGridView1.Columns[7].HeaderCell.Value = "住址";
dataGridView1.Columns[8].HeaderCell.Value = "电话";
}
private void 职工信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 信息录入ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlcmd.Connection = conn;
sqlcmd.CommandText = "xxlr";
sqlcmd.Parameters.Add("@职工号", SqlDbType.VarChar, 10).Value = textBox2.Text;
sqlcmd.Parameters.Add("@姓名", SqlDbType.VarChar, 10).Value = textBox1.Text;
sqlcmd.Parameters.Add("@性别", SqlDbType.VarChar, 2).Value = textBox8.Text;
sqlcmd.Parameters.Add("@出生年月", SqlDbType.VarChar, 20).Value = textBox7.Text;
sqlcmd.Parameters.Add("@职务", SqlDbType.VarChar, 15).Value = textBox6.Text;
sqlcmd.Parameters.Add("@学历", SqlDbType.VarChar, 10).Value = textBox9.Text;
sqlcmd.Parameters.Add("@工资", SqlDbType.Int).Value = Convert.ToInt32(textBox3.Text);
sqlcmd.Parameters.Add("@住址", SqlDbType.VarChar, 10).Value = textBox5.Text;
sqlcmd.Parameters.Add("@电话", SqlDbType.VarChar, 11).Value = textBox4.Text;
if (sqlcmd.ExecuteNonQuery() >= 1)
{
MessageBox.Show("职工信息录入成功", "好");
}
conn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void 信息显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr,sql;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "select * from 职工信息汇总表";
//SqlCommand sqlcmd = new SqlCommand(sql,conn);
bs = new BindingSource();
SqlDataAdapter dr = new SqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
dr.Fill(myds);
bs.DataSource = myds.Tables[0];
this.dataGridView1.DataSource = bs;
dataGridView1.Columns[0].HeaderCell.Value = "职工号";
dataGridView1.Columns[1].HeaderCell.Value = "名字";
dataGridView1.Columns[2].HeaderCell.Value = "性别";
dataGridView1.Columns[3].HeaderCell.Value = "出生日期";
dataGridView1.Columns[4].HeaderCell.Value = "学历";
dataGridView1.Columns[5].HeaderCell.Value = "职务";
dataGridView1.Columns[6].HeaderCell.Value = "工资";
dataGridView1.Columns[7].HeaderCell.Value = "住址";
dataGridView1.Columns[8].HeaderCell.Value = "电话";
}
private void 按工号ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr, sql,gh;
gh = textBox2.Text;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "select * from 职工信息汇总表 where 职工号='"+gh+"'";
//SqlCommand sqlcmd = new SqlCommand(sql,conn);
bs = new BindingSource();
SqlDataAdapter dr = new SqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
dr.Fill(myds);
bs.DataSource = myds.Tables[0];
this.dataGridView1.DataSource = bs;
dataGridView1.Columns[0].HeaderCell.Value = "职工号";
dataGridView1.Columns[1].HeaderCell.Value = "名字";
dataGridView1.Columns[2].HeaderCell.Value = "性别";
dataGridView1.Columns[3].HeaderCell.Value = "出生日期";
dataGridView1.Columns[4].HeaderCell.Value = "学历";
dataGridView1.Columns[5].HeaderCell.Value = "职务";
dataGridView1.Columns[6].HeaderCell.Value = "工资";
dataGridView1.Columns[7].HeaderCell.Value = "住址";
dataGridView1.Columns[8].HeaderCell.Value = "电话";
}
private void 按学历ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr, sql, xl;
xl= textBox9.Text;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "select * from 职工信息汇总表 where 学历='" + xl+ "'";
//SqlCommand sqlcmd = new SqlCommand(sql,conn);
bs = new BindingSource();
SqlDataAdapter dr = new SqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
dr.Fill(myds);
bs.DataSource = myds.Tables[0];
this.dataGridView1.DataSource = bs;
dataGridView1.Columns[0].HeaderCell.Value = "职工号";
dataGridView1.Columns[1].HeaderCell.Value = "名字";
dataGridView1.Columns[2].HeaderCell.Value = "性别";
dataGridView1.Columns[3].HeaderCell.Value = "出生日期";
dataGridView1.Columns[4].HeaderCell.Value = "学历";
dataGridView1.Columns[5].HeaderCell.Value = "职务";
dataGridView1.Columns[6].HeaderCell.Value = "工资";
dataGridView1.Columns[7].HeaderCell.Value = "住址";
dataGridView1.Columns[8].HeaderCell.Value = "电话";
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
}
private void 按职务ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connstr, sql, zw;
zw = textBox6.Text;
connstr = "Server=LAPTOP-EFMKQ5FV;User Id=sa;Password=123;Database=职工信息管理系统;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
sql = "select * from 职工信息汇总表 where 职务 like'%" + zw + "%'";
//SqlCommand sqlcmd = new SqlCommand(sql,conn);
BindingSource bs = new BindingSource();
SqlDataAdapter dr = new SqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
dr.Fill(myds);
bs.DataSource = myds.Tables[0];
this.dataGridView1.DataSource = bs;
dataGridView1.Columns[0].HeaderCell.Value = "职工号";
dataGridView1.Columns[1].HeaderCell.Value = "名字";
dataGridView1.Columns[2].HeaderCell.Value = "性别";
dataGridView1.Columns[3].HeaderCell.Value = "出生日期";
dataGridView1.Columns[4].HeaderCell.Value = "学历";
dataGridView1.Columns[5].HeaderCell.Value = "职务";
dataGridView1.Columns[6].HeaderCell.Value = "工资";
dataGridView1.Columns[7].HeaderCell.Value = "住址";
dataGridView1.Columns[8].HeaderCell.Value = "电话";
}
private void toolStripStatusLabel1_Click_1(object sender, EventArgs e)
{
}
private void toolStripStatusLabel2_Click(object sender, EventArgs e)
{
bs.MoveFirst();
}
private void toolStripStatusLabel3_Click(object sender, EventArgs e)
{
bs.MovePrevious();
}
private void toolStripStatusLabel4_Click(object sender, EventArgs e)
{
bs.MoveNext();
}
private void toolStripStatusLabel5_Click(object sender, EventArgs e)
{
bs.MoveLast();
}
private void 或缺所选行信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
int hs = dataGridView1.CurrentRow.Index;
textBox2.Text = dataGridView1.Rows[hs].Cells[0].Value.ToString();
textBox1.Text = dataGridView1.Rows[hs].Cells[1].Value.ToString();
textBox8.Text = dataGridView1.Rows[hs].Cells[2].Value.ToString();
textBox7.Text = dataGridView1.Rows[hs].Cells[3].Value.ToString();
textBox9.Text = dataGridView1.Rows[hs].Cells[4].Value.ToString();
textBox6.Text = dataGridView1.Rows[hs].Cells[5].Value.ToString();
textBox3.Text = dataGridView1.Rows[hs].Cells[6].Value.ToString();
textBox5.Text = dataGridView1.Rows[hs].Cells[7].Value.ToString();
textBox4.Text = dataGridView1.Rows[hs].Cells[8].Value.ToString();
}
}
}
microsoftsql配置
存储过程
USE [职工信息管理系统]
GO
/****** Object: StoredProcedure [dbo].[xxlr] Script Date: 2022/4/30 22:58:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[xxlr] (
@职工号 varchar(10),
@姓名 varchar(10),
@性别 varchar(2),
@出生年月 varchar(20),
@学历 varchar(10),
@职务 varchar(15),
@工资 int,
@住址 varchar(10),
@电话 varchar(11)
)
as
insert into 职工信息汇总表 (职工号,姓名,性别,出生年月,学历,职务,工资,住址,电话)
values (@职工号,@姓名,@性别,@出生年月,@学历,@职务,@工资,@住址,@电话)
//插入的
USE [职工信息管理系统]
GO
/****** Object: StoredProcedure [dbo].[xxxg] Script Date: 2022/4/30 22:59:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[xxxg](
@职工号 varchar(10),
@姓名 varchar(10),
@性别 varchar(2),
@出生年月 varchar(20),
@学历 varchar(10),
@职务 varchar(15),
@工资 int,
@住址 varchar(10),
@电话 varchar(11)
)
as
update 职工信息汇总表
SET 姓名=@姓名,性别=@性别,出生年月=@出生年月,学历=@学历,职务=@职务,工资=@工资,住址=@住址,电话=@电话
where 职工号=@职工号
//修改的
演示
全显示
获取当前行的信息
下面的菜单是给数据导航的
基本的增删改查和按工号和学历已经按职务的模糊查询都可以
因为不太熟练,而且作为一个二年级混子,这种管理系统也花了我4个小时左右,嗯,我不应该在敲代码时老是去选BGM。