2022年C#实验报告 .pdf
C#实验报告(人事管理系统)学号149074353 姓名程裕博班级物 141 指导教师夏敏安徽工业大学计算机学院2016 年 12 月名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 29 页 - - - - - - - - - 一程序步骤先设置一个登录界面(连接数据库),登录时进行判断,根据不同的错误信息进行报错,如密码错误。 登陆成功后, 进入主界面。 主界面通过树和表格来显示数据。可以对人员, 班组,部门进行增删改查操作,数据保存在数据库中。二程序源码1.数据库表结构名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 29 页 - - - - - - - - - 2、登录界面using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CCWin; using System.Data.SqlClient; namespace 实验_人事管理系统 publicpartialclassForm_login : CCSkinMain public Form_login() InitializeComponent(); string src = 博博实验人事管理系统实验人事管理系统MyData.mdf;Integrated Security=True; privatevoid Form1_Load( object sender, EventArgs e) privatevoid button_login_Click(object sender, EventArgs e) string user = this .skinTextBox.Text; string password = this .skinTextBox2.Text; int sign1 = 0; int sign2 = 0; if (user = | password = ) MessageBox .Show( 您输入的数据不完整! ); else string sql1 = string.Format( select USERNAME from Table_login where USERNAME=N0;, user); using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql1, coon); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 29 页 - - - - - - - - - coon.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() sign1+; if (sign1 = 0) MessageBox .Show( 未查询到您的用户信息!); if (sign1 != 0) string sql2 = string.Format( select PASSWORD from Table_login where USERNAME=N0;, user); using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql2, coon); coon.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() if (password = drPASSWORD.ToString() sign2+; if (sign1 != 0 & sign2 = 0) MessageBox .Show( 您的密码输入错误! ); if (sign2 != 0) Form_show f1 = newForm_show (user, this ); f1.Show(); this .Visible = false ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 29 页 - - - - - - - - - 3、主界面using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Timers; using System.Data.SqlClient; using CCWin; namespace 实验_人事管理系统 publicpartialclassForm_show : Form public Form_show() InitializeComponent(); public Form_show( string name1, Form_login f) login_name=name1; fl = f; InitializeComponent(); string login_name= xx ; Form_login fl =null ; TreeNode t = newTreeNode(); int ID=-1; public List list = new List (); string name; string birth; string sex; string education; string adminisitrator; string manegetime; / string category_name;string src = 博博实验人事管理系统实验人事管理系统MyData.mdf;Integrated Security=True; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 29 页 - - - - - - - - - privatevoid Form_show_Load(object sender, EventArgs e) toolStripStatusLabel_name.Text = 管理员: + this .login_name; toolStripStatusLabel_time.Text = 登录时间: + DateTime.Now.ToLongTimeString(); Addparent(); this .dataGridView1.ClearSelection(); privatevoid toolStripStatusLabel_name_Click(object sender, EventArgs e) privatevoid Form_show_FormClosed(object sender, FormClosedEventArgs e) fl.Close(); publicvoid Addparent() string sql = select * from Table_tree where PR_NUMBER=-1; using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql, coon); coon.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() string name = drNAME .ToString(); string number = drNUMBER .ToString(); / string category=drCATEGORY.ToString();TreeNode tn = newTreeNode(); tn.Text = name; tn.Tag = number; tn.ImageIndex = 1; AddChild(number, tn); treeView_jiegou.Nodes.Add(tn); / skinTreeView1.Nodes.Add(tn); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 29 页 - - - - - - - - - privatevoid AddChild( string pnumber, TreeNode pnode) if ( Convert .ToInt32(pnode.Tag) = 0 | (pnode.Parent!=null & Convert .ToInt32(pnode.Parent.Tag) = 0) stringsql = select * from Table_tree where PR_NUMBER= + pnumber + order by NAME desc; ; using ( SqlConnection conn = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql, conn); conn.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() string name = drNAME .ToString(); string number = drNUMBER .ToString(); / string category = drCATEGORY.ToString();TreeNode tn = newTreeNode(); tn.Text = name; tn.Tag = number; tn.ImageIndex = 1; AddChild(number, tn); pnode.Nodes.Add(tn); else stringsql = select * from Table_tree where PR_NUMBER= + pnumber + order by NAME desc; ; using ( SqlConnection conn = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql, conn); conn.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() string name = drNAME .ToString(); string number = drNUMBER .ToString(); / string category = drCATEGORY.ToString();名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 29 页 - - - - - - - - - TreeNode tn = newTreeNode(); tn.Text = name; tn.Tag = number; tn.ImageIndex = 0; AddChild(number, tn); pnode.Nodes.Add(tn); privatevoid skinButton_add_Click(object sender, EventArgs e) / 无用的按钮 privatevoid skinButton_add_Click_1(object sender, EventArgs e) /MessageBox.Show( 添加! );if (t.Parent.Parent!=null & t.Parent!=null ) Form_Addperson fp = newForm_Addperson(t, toolStripStatusLabel_name.Text, DateTime.Now.ToString(); fp.Owner = this ; fp.Show(); list.Clear(); elseMessageBox .Show( 请到班组添加人员! ); / reshow(); privatevoid treeView_jiegou_AfterSelect(object sender, TreeViewEventArgs e) string sql = select * from Table_person where CATEGORY_NUMBER= + e.Node.Tag; List list = newList (); using ( SqlConnection conn = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql, conn); conn.Open(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 29 页 - - - - - - - - - using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() person p = newperson (); p.Person_name = drPERSON_NAME.ToString(); p.Category_name = drCATEGORY_NAME.ToString(); p.Sex = drSEX.ToString(); string dt=drBIRTH.ToString().Split( ); p.Birth = dt0; p.Education = drEDUCATION .ToString(); p.Adminisitrator = drADMINISITRATOR .ToString(); p.Manegetime=drMANEGETIME.ToString(); list.Add(p); this .dataGridView1.ClearSelection(); this .dataGridView1.DataSource = list; ID = -1; privatevoid添加部门 ToolStripMenuItem_Click(object sender, EventArgs e) if (t.Parent = null | t.Parent.Parent = null ) Form_Add fa = newForm_Add (t); fa.Owner = this ; fa.Show(); else Form_Addperson fp = newForm_Addperson(t, toolStripStatusLabel_name.Text, DateTime.Now.ToString(); fp.Owner = this ; fp.Show(); list.Clear(); this .dataGridView1.DataSource = list; privatevoid treeView_jiegou_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 29 页 - - - - - - - - - this .t = e.Node; / MessageBox.Show(t.Name); publicvoid reshow() treeView_jiegou.Nodes.Clear(); Addparent(); privatevoid dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) /string name;/string birth;/string education;/string adminisitrator;/string manegetime; if (e.RowIndex != -1) name = this .dataGridView1.Rowse.RowIndex.Cells0.Value.ToString(); birth = this .dataGridView1.Rowse.RowIndex.Cells2.Value.ToString(); education = this .dataGridView1.Rowse.RowIndex.Cells4.Value.ToString(); adminisitrator = this .dataGridView1.Rowse.RowIndex.Cells5.Value.ToString(); manegetime = this .dataGridView1.Rowse.RowIndex.Cells6.Value.ToString(); sex = this .dataGridView1.Rowse.RowIndex.Cells3.Value.ToString(); /MessageBox.Show(name+birth+education+adminisitrator+manegetime+sex);string sql = string.Format( select ID from Table_person where PERSON_NAME=N0 and BIRTH=N1 and ADMINISITRATOR=N2 and MANEGETIME=N3, name, birth, adminisitrator, manegetime); / MessageBox.Show(sql);using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql, coon); coon.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() ID = (int )dr ID ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 29 页 - - - - - - - - - privatevoid skinButton_delete_Click(object sender, EventArgs e) if (ID != -1) string sql = delete from Table_person where ID=+ID.ToString()+; ; using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql,coon); coon.Open(); int n=cmd.ExecuteNonQuery(); if (n=1) MessageBox .Show( 删除成功! ); reshowview(); reshow(); /foreach (person p in list)/ if (p.Adminisitrator = adminisitrator & p.Birth = birth & p.Person_name = name & p.Manegetime = manegetime)/ / list.Remove(p);/ /this.dataGridView1.DataSource = list; else MessageBox .Show( 未选定行! ); privatevoid skinButton_rewrite_Click(object sender, EventArgs e) if (ID != -1) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 29 页 - - - - - - - - - Form_rewrite fr = new Form_rewrite (name, sex, birth, education, toolStripStatusLabel_name.Text, manegetime, ID); fr.Owner = this ; fr.Show(); elseMessageBox .Show( 未选定行! ); /list.Clear();/ this.dataGridView1.DataSource=list; privatevoid skinButton1_search_Click(object sender, EventArgs e) string sql = select * from Table_person where 1=1; List list1 = newList (); / list1.Clear();if ( this .textBox1.Text.Trim() != ) sql = sql + and PERSON_NAME like N%+ this .textBox1.Text.Trim()+ % ; if ( this .textBox2.Text.Trim() != ) sql = sql + and CATEGORY_NAME like N% + this .textBox2.Text.Trim()+ % ; sql += ; ; using ( SqlConnection conn = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql, conn); conn.Open(); using ( SqlDataReader dr = cmd.ExecuteReader() while (dr.Read() person p = newperson (); p.Person_name = drPERSON_NAME.ToString(); p.Category_name = drCATEGORY_NAME.ToString(); p.Sex = drSEX.ToString(); string dt = drBIRTH.ToString().Split( ); p.Birth = dt0; p.Education = drEDUCATION .ToString(); p.Adminisitrator = drADMINISITRATOR .ToString(); p.Manegetime = drMANEGETIME.ToString(); list1.Add(p); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 29 页 - - - - - - - - - this .dataGridView1.ClearSelection(); this .dataGridView1.DataSource = list1; privatevoid skinButton_reset_Click(object sender, EventArgs e) this .textBox1.Text = ; this .textBox2.Text = ; List list1 = newList (); this .dataGridView1.DataSource = list1; publicvoid reshowview() List list1 = newList (); list1.Clear(); this .dataGridView1.DataSource = list1; privatevoid删除部门 ToolStripMenuItem_Click(object sender, EventArgs e) Form_delete fm_delete = new Form_delete (t); fm_delete.Owner = this ; fm_delete.Show(); privatevoid修改单位 ToolStripMenuItem_Click(object sender, EventArgs e) Form_rename rename = newForm_rename (t, t.Text.ToString(); rename.Owner = this ; rename.Show(); 4、人员类using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 29 页 - - - - - - - - - namespace 实验_人事管理系统 publicclassperson privatestring person_name; publicstring Person_name get return person_name; set person_name = value ; privatestring category_name; publicstring Category_name get return category_name; set category_name = value ; privatestring birth; publicstring Birth get return birth; set birth = value ; privatestring sex; publicstring Sex get return sex; set sex = value ; privatestring education; publicstring Education get return education; set education = value ; privatestring adminisitrator; publicstring Adminisitrator get return adminisitrator; set adminisitrator = value ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 29 页 - - - - - - - - - privatestring manegetime; publicstring Manegetime get return manegetime; set manegetime = value ; 5、删除界面using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using CCWin; namespace 实验_人事管理系统 publicpartialclassForm_delete : CCSkinMain public Form_delete() InitializeComponent(); public Form_delete(TreeNode node) InitializeComponent(); this .t = node; TreeNode t = newTreeNode(); string src = 博博实验人事管理系统实验人事管理系统MyData.mdf;Integrated Security=True; privatevoid Form_delete_Load( object sender, EventArgs e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 29 页 - - - - - - - - - privatevoid skinButton_sure_Click(object sender, EventArgs e) if (t != null & t.Parent != null ) if (t.Parent.Tag.ToString() = 0 ) / /ox.Show(t.Tag.ToString()+:+t.Text);string sql1 = delete from Table_person where CATEGORY_NUMBER in(select NUMBER from Table_tree where PR_NUMBER in(select NUMBER from Table_tree where NAME=N+ t.Text + ); / MessageBox.Show(sql1);using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql1, coon); coon.Open(); int n = cmd.ExecuteNonQuery(); / MessageBox.Show(您已经删除了 + n.ToString() + 条数据! ); string sql2 = delete from Table_tree where PR_NUMBER in(select NUMBER from Table_tree where NAME=N + t.Text + ); / MessageBox.Show(sql2);using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql2, coon); coon.Open(); int n = cmd.ExecuteNonQuery(); / MessageBox.Show(您已经删除了 + n.ToString() + 条数据! ); string sql3 = delete from Table_tree where NAME=N + t.Text + ; / MessageBox.Show(sql3);using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql3, coon); coon.Open(); int n = cmd.ExecuteNonQuery(); / MessageBox.Show(您已经删除了 + n.ToString() + 条数据! ); else string sql4 = delete from Table_person where CATEGORY_NUMBER = + t.Tag + ; ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 29 页 - - - - - - - - - string sql5 = delete from Table_tree where NUMBER = + t.Tag + ; ; / MessageBox.Show(sql4);using ( SqlConnection coon = newSqlConnection (src) SqlCommand cmd = newSqlCommand (sql4, coon); coon.Open(); int n = cmd.ExecuteNonQuery(); / MessageBox.Show(您已经删除了 + n.ToString() + 条数据! );SqlCommand cmd1 = new SqlCommand (sql5, coon); int n1 = cmd1.ExecuteNonQuery(); / MessageBox.Show(您已经删除了 + n.ToString() + 条数据! ); Form_show ff = newForm_show (); ff = (Form_show ) this .Owner; ff.reshow(); this .Close(); privatevoid skinButton_giveup_Click(object sender, EventArgs e) this .Close(); 5、添加界面(部门,班组)using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using Sys