当前位置:网站首页>C#与MySQL数据库连接
C#与MySQL数据库连接
2022-07-02 06:26:00 【寂云萧】
新建一个C#类库,用于与MySQL数据进行连接。其主要访问方式为:
在登录了数据库之后,可以使用SQL语句对数据库进行操作。
包括增、删、改以及查询。
一、需要导入的类库
using MySql.Data.MySqlClient;
using System.Data;
二、主要代码
class mysqlDB
{
//数据库连接
//只需要登陆一次,将返回的对象传入下面的函数即可
public MySqlConnection connetion(string server, string user, string passwd)
{
String conStr = string.Format("server={0};user id={1};password={2};database=userdata", server, user, passwd);
MySqlConnection DBcon = new MySqlConnection();
DBcon.ConnectionString = conStr;
DBcon.Open();
return DBcon;
}
//数据库操作
//不需要额外调用
public MySqlCommand command(string sql, MySqlConnection DBconn)
{
MySqlCommand rt = new MySqlCommand(sql, DBconn);
return rt;
}
//数据库增删改
public int Exute(string sql, MySqlConnection DBconn)
{
//返回受影响的行数
return this.command(sql, DBconn).ExecuteNonQuery();
}
//通过select语句进行查询
public MySqlDataReader read(string sql, MySqlConnection DBconn)
{
//数据库查询
return this.command(sql, DBconn).ExecuteReader();
}
}
三、调用方式
假设我有一个数据库叫userdata,数据库下有一个表格data,设置如下
| id | name | class |
|---|---|---|
| INT | varchar | varchar |
查询
查询调用的是MySqlDataReader函数。
//数据库查询函数
private Tu DBshow()
{
mysqlDB mysql = new mysqlDB();
string sql = "SELECT * FROM userdata.data;";
//DBconn为登录之后返回的变量
IDataReader dr = mysql.read(sql, DBconn);
while (dr.Read())
{
string id, userName, Class;
id = dr["id"].ToString();
userName = dr["name"].ToString();
Class = dr["class"].ToString();
string[] str = {
id, userName, C88lass };
this.DataGridView1.Rows.Add(str);
}
dr.Close();
}
增删改
包括但不限于增删改都是使用Exute函数。除了增删改,建表、删表等操作也可以使用这个函数来执行。
使用方法与查询类似。
//增添信息
insert = "insert into userdata.data (`id`,`name`,`Class`) values('0','张三','一班');";
//删除
del = "DELETE FROM userdata.data WHERE (`id` = '0');";
//修改
update = "UPDATE userdata.data SET `name` = '李四' WHERE (`id` = '0');"
将这三条语句分别带入就可以实现对应的操作,修改x条信息,如果修改成功就返回一个整数值x。
mysqlDB mysql = new mysqlDB();
mysql.Exute(sql语句, DBconn);
边栏推荐
- MMDetection安装问题
- 【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
- ModuleNotFoundError: No module named ‘pytest‘
- Handwritten call, apply, bind
- Using compose to realize visible scrollbar
- open3d学习笔记五【RGBD融合】
- Open failed: enoent (no such file or directory) / (operation not permitted)
- PHP returns the abbreviation of the month according to the numerical month
- MMDetection模型微调
- [mixup] mixup: Beyond Imperial Risk Minimization
猜你喜欢
![[CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video](/img/bc/c54f1f12867dc22592cadd5a43df60.png)
[CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video

PointNet原理证明与理解

Alpha Beta Pruning in Adversarial Search
![[model distillation] tinybert: distilling Bert for natural language understanding](/img/c1/e1c1a3cf039c4df1b59ef4b4afbcb2.png)
[model distillation] tinybert: distilling Bert for natural language understanding

常见CNN网络创新点

【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》

Traditional target detection notes 1__ Viola Jones

What if the notebook computer cannot run the CMD command

【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》

【双目视觉】双目立体匹配
随机推荐
Win10+vs2017+denseflow compilation
Thesis writing tip2
Jordan decomposition example of matrix
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》
Latex formula normal and italic
Huawei machine test questions
【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
【Batch】learning notes
【MagNet】《Progressive Semantic Segmentation》
conda常用命令
机器学习理论学习:感知机
【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
Regular expressions in MySQL
Win10 solves the problem that Internet Explorer cannot be installed
open3d学习笔记二【文件读写】
Thesis tips
Calculate the total in the tree structure data in PHP
【Cascade FPD】《Deep Convolutional Network Cascade for Facial Point Detection》
Alpha Beta Pruning in Adversarial Search