C#_使用ADO.NET实体模型与数据库进行快速连接

简介: C#_使用ADO.NET实体模型与数据库进行快速连接,实现增、删、改、查操作

数据库文件与源程序获取

数据库文件

百度云:

链接: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

可以对比以下两篇文章

相关文章
|
9月前
|
机器学习/深度学习 算法 定位技术
Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现裂缝的检测识别(C#代码UI界面版)
本项目基于YOLOv8模型与C#界面,结合Baumer工业相机,实现裂缝的高效检测识别。支持图像、视频及摄像头输入,具备高精度与实时性,适用于桥梁、路面、隧道等多种工业场景。
1144 27
|
关系型数据库 MySQL Java
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
|
7月前
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
327 13
|
7月前
|
SQL 关系型数据库 MySQL
MySQL数据库连接过多(Too many connections)错误处理策略
综上所述,“Too many connections”错误处理策略涉及从具体参数配置到代码层面再到系统与架构设计全方位考量与改进。每项措施都需根据具体环境进行定制化调整,并且在执行任何变更前建议先行测试评估可能带来影响。
1639 11
|
安全 Linux 网络安全
YashanDB数据库服务端SSL连接配置
YashanDB支持通过SSL连接确保数据传输安全,需在服务端生成根证书、服务器证书及DH文件,并将根证书提供给客户端以完成身份验证。服务端配置包括使用OpenSSL工具生成证书、设置SSL参数并重启数据库;客户端则需下载根证书并正确配置环境变量与`yasc_env.ini`文件。注意:启用SSL后,所有客户端必须持有根证书才能连接,且SSL与密码认证独立运行。
|
9月前
|
SQL XML Java
配置Spring框架以连接SQL Server数据库
最后,需要集成Spring配置到应用中,这通常在 `main`方法或者Spring Boot的应用配置类中通过加载XML配置或使用注解来实现。
691 0
|
Oracle 安全 关系型数据库
【Oracle】使用Navicat Premium连接Oracle数据库两种方法
以上就是两种使用Navicat Premium连接Oracle数据库的方法介绍,希望对你有所帮助!
2391 28
|
SQL 数据库连接 数据库
在C++的QT框架中实现SQLite数据库的连接与操作
以上就是在C++的QT框架中实现SQLite数据库的连接与操作的基本步骤。这些步骤包括创建数据库连接、执行SQL命令、处理查询结果和关闭数据库连接。在实际使用中,你可能需要根据具体的需求来修改这些代码。
772 14
下一篇
开通oss服务