当前位置:网站首页>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);
边栏推荐
- PPT的技巧
- open3d学习笔记二【文件读写】
- Yolov3 trains its own data set (mmdetection)
- 【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
- 生成模型与判别模型的区别与理解
- Record of problems in the construction process of IOD and detectron2
- Feature Engineering: summary of common feature transformation methods
- 聊天中文语料库对比(附上各资源链接)
- PHP returns the abbreviation of the month according to the numerical month
- Faster-ILOD、maskrcnn_ Benchmark training coco data set and problem summary
猜你喜欢

【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》

【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》

Faster-ILOD、maskrcnn_benchmark训练coco数据集及问题汇总

TimeCLR: A self-supervised contrastive learning framework for univariate time series representation

【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》

【Programming】

What if a new window always pops up when opening a folder on a laptop

Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations

Thesis writing tip2

How do vision transformer work?【论文解读】
随机推荐
latex公式正体和斜体
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
ModuleNotFoundError: No module named ‘pytest‘
A slide with two tables will help you quickly understand the target detection
The difference and understanding between generative model and discriminant model
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》
Huawei machine test questions-20190417
Conversion of numerical amount into capital figures in PHP
【MagNet】《Progressive Semantic Segmentation》
Huawei machine test questions
常见CNN网络创新点
Drawing mechanism of view (3)
How do vision transformer work? [interpretation of the paper]
TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
[introduction to information retrieval] Chapter 7 scoring calculation in search system
PointNet理解(PointNet实现第4步)
Point cloud data understanding (step 3 of pointnet Implementation)
程序的内存模型
How to turn on night mode on laptop
【双目视觉】双目立体匹配