当前位置:网站首页>C # connect to MySQL database
C # connect to MySQL database
2022-07-02 07:53:00 【Silent clouds】
Create a new one C# Class library , Used with MySQL Data to connect . Its main access mode is :
After logging into the database , have access to SQL Statement to operate the database .
Including increase 、 Delete 、 Modification and query .
One 、 Class libraries to be imported
using MySql.Data.MySqlClient;
using System.Data;
Two 、 Main code
class mysqlDB
{
// Database connection
// Just log in once , Pass the returned object into the following function
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;
}
// Database operation
// No need for extra calls
public MySqlCommand command(string sql, MySqlConnection DBconn)
{
MySqlCommand rt = new MySqlCommand(sql, DBconn);
return rt;
}
// Database addition, deletion and modification
public int Exute(string sql, MySqlConnection DBconn)
{
// Returns the number of affected rows
return this.command(sql, DBconn).ExecuteNonQuery();
}
// adopt select Statement to query
public MySqlDataReader read(string sql, MySqlConnection DBconn)
{
// Database query
return this.command(sql, DBconn).ExecuteReader();
}
}
3、 ... and 、 Call mode
Suppose I have a database called userdata, There is a table under the database data, Set as follows
| id | name | class |
|---|---|---|
| INT | varchar | varchar |
Inquire about
The query calls MySqlDataReader function .
// Database query function
private Tu DBshow()
{
mysqlDB mysql = new mysqlDB();
string sql = "SELECT * FROM userdata.data;";
//DBconn Is the variable returned after login
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();
}
Additions and deletions
Including but not limited to addition, deletion and modification Exute function . Except for addition, deletion and modification , Build table 、 You can also use this function to perform operations such as deleting tables .
The use method is similar to query .
// Add information
insert = "insert into userdata.data (`id`,`name`,`Class`) values('0',' Zhang San ',' Class one ');";
// Delete
del = "DELETE FROM userdata.data WHERE (`id` = '0');";
// modify
update = "UPDATE userdata.data SET `name` = ' Li Si ' WHERE (`id` = '0');"
The corresponding operations can be realized by bringing these three statements in separately , modify x Bar information , If the modification is successful, an integer value is returned x.
mysqlDB mysql = new mysqlDB();
mysql.Exute(sql sentence , DBconn);
边栏推荐
- ModuleNotFoundError: No module named ‘pytest‘
- [in depth learning series (8)]: principles of transform and actual combat
- mmdetection训练自己的数据集--CVAT标注文件导出coco格式及相关操作
- Common CNN network innovations
- 【雙目視覺】雙目矯正
- Gensim如何冻结某些词向量进行增量训练
- Implementation of yolov5 single image detection based on pytorch
- 用MLP代替掉Self-Attention
- 【Cascade FPD】《Deep Convolutional Network Cascade for Facial Point Detection》
- Mmdetection model fine tuning
猜你喜欢

论文写作tip2

【MagNet】《Progressive Semantic Segmentation》

【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》

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

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

Network metering - transport layer

【学习笔记】Matlab自编图像卷积函数

【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》

基于pytorch的YOLOv5单张图片检测实现

【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
随机推荐
Memory model of program
Nacos service registration in the interface
MMDetection安装问题
conda常用命令
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
[binocular vision] binocular stereo matching
open3d环境错误汇总
【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
CVPR19-Deep Stacked Hierarchical Multi-patch Network for Image Deblurring论文复现
Implementation of yolov5 single image detection based on pytorch
【Random Erasing】《Random Erasing Data Augmentation》
生成模型与判别模型的区别与理解
Calculate the difference in days, months, and years between two dates in PHP
Open3D学习笔记一【初窥门径,文件读取】
Win10 solves the problem that Internet Explorer cannot be installed
【深度学习系列(八)】:Transoform原理及实战之原理篇
mmdetection训练自己的数据集--CVAT标注文件导出coco格式及相关操作
Deep learning classification Optimization Practice
Thesis writing tip2
ABM thesis translation