数据库文件与源程序获取
数据库文件
百度云:
链接:https://pan.baidu.com/s/1FHWdyebUOZoAvKXycvFi3g
提取码:im4l
源程序
github:
https://github.com/GitHub-yiming/WindowsFormsAppADO.NET.git
代码展示
一共有两个界面,Form1为主界面,Form2为信息添加界面
主界面
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Configuration; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; namespaceWindowsFormsAppADO.NET{ publicpartialclassForm1 : Form { Calorimeter1Entitiesdb=newCalorimeter1Entities(); publicForm1() { InitializeComponent(); } privatevoidForm1_Load(objectsender, EventArgse) { using (vardb=newCalorimeter1Entities()) { tableNumbers1BindingSource.DataSource=db.Table_Numbers1.ToList(); } } //添加privatevoidbutton1_Click(objectsender, EventArgse) { using(Form2form2=newForm2(null)) { form2.ShowDialog(); using (vardb=newCalorimeter1Entities()) { tableNumbers1BindingSource.DataSource=db.Table_Numbers1.ToList(); } } } //刷新privatevoidbutton2_Click(objectsender, EventArgse) { using (vardb=newCalorimeter1Entities()) { tableNumbers1BindingSource.DataSource=db.Table_Numbers1.ToList(); } } //编辑privatevoidbutton3_Click(objectsender, EventArgse) { using (Form2form2=newForm2(tableNumbers1BindingSource.CurrentasTable_Numbers1)) { form2.ShowDialog(); using (vardb=newCalorimeter1Entities()) { tableNumbers1BindingSource.DataSource=db.Table_Numbers1.ToList(); } } } //删除privatevoidbutton4_Click(objectsender, EventArgse) { using(vardb=newCalorimeter1Entities()) { //int i = dataGridView1.SelectedRows.Count;tableNumbers1BindingSource.DataSource=db.Table_Numbers1.ToList(); if (tableNumbers1BindingSource.Current!=null) { if (MessageBox.Show("你确定要删除这条数据吗?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==DialogResult.Yes) { db.Table_Numbers1.Remove(tableNumbers1BindingSource.CurrentasTable_Numbers1); tableNumbers1BindingSource.RemoveCurrent(); db.SaveChanges(); } } } } privatevoiddataGridView1_RowPostPaint(objectsender, DataGridViewRowPostPaintEventArgse) { try { //添加行号 SolidBrushv_SolidBrush=newSolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor); intv_LineNo=0; v_LineNo=e.RowIndex+1; stringv_Line=v_LineNo.ToString(); e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X+15, e.RowBounds.Location.Y+5); } catch (Exceptionex) { MessageBox.Show("添加行号时发生错误,错误信息:"+ex.Message, "操作失败"); } } } }
信息添加界面
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; namespaceWindowsFormsAppADO.NET{ publicpartialclassForm2 : Form { Calorimeter1Entitiesdb=newCalorimeter1Entities(); publicForm2(Table_Numbers1obj) { InitializeComponent(); if(obj==null) { tableNumbers1BindingSource.DataSource=newTable_Numbers1(); db.Table_Numbers1.Add(tableNumbers1BindingSource.CurrentasTable_Numbers1); } else { tableNumbers1BindingSource.DataSource=obj; db.Table_Numbers1.Attach(tableNumbers1BindingSource.CurrentasTable_Numbers1); } } privatevoidForm2_FormClosing(objectsender, FormClosingEventArgse) { if (DialogResult==DialogResult.OK) { db.SaveChanges(); e.Cancel=false; } e.Cancel=false; } } }
最后如果有什么问题,欢迎在下方评论,我看到会及时回复的。
有兴趣的话可以看看的另一篇文章:
C#&SQL Server基于三层架构实现增删改查
https://developer.aliyun.com/article/830527?spm=a2c6h.13148508.0.0.6d814f0eE0mJgO
可以对比以下两篇文章