当前位置:网站首页>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);
边栏推荐
- Faster-ILOD、maskrcnn_ Benchmark installation process and problems encountered
- ABM thesis translation
- 【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
- 利用Transformer来进行目标检测和语义分割
- Remplacer l'auto - attention par MLP
- 图片数据爬取工具Image-Downloader的安装和使用
- 【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
- 【Batch】learning notes
- [Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image
- 【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》
猜你喜欢
win10+vs2017+denseflow编译
[Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image
用全连接层替代掉卷积 -- RepMLP
【学习笔记】Matlab自编图像卷积函数
CVPR19-Deep Stacked Hierarchical Multi-patch Network for Image Deblurring论文复现
程序的内存模型
[binocular vision] binocular correction
【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》
Common machine learning related evaluation indicators
Proof and understanding of pointnet principle
随机推荐
【Paper Reading】
Conversion of numerical amount into capital figures in PHP
用MLP代替掉Self-Attention
ABM thesis translation
What if the laptop can't search the wireless network signal
【Random Erasing】《Random Erasing Data Augmentation》
Mmdetection trains its own data set -- export coco format of cvat annotation file and related operations
Win10 solves the problem that Internet Explorer cannot be installed
Ppt skills
Sorting out dialectics of nature
MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
open3d学习笔记五【RGBD融合】
论文tips
How to turn on night mode on laptop
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
Determine whether the version number is continuous in PHP
【双目视觉】双目立体匹配
【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》
利用Transformer来进行目标检测和语义分割
Calculate the total in the tree structure data in PHP