当前位置:网站首页>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);
边栏推荐
- Latex formula normal and italic
- Handwritten call, apply, bind
- latex公式正体和斜体
- 【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》
- ABM thesis translation
- conda常用命令
- [tricks] whiteningbert: an easy unsupervised sentence embedding approach
- [CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
猜你喜欢

Tencent machine test questions

Common machine learning related evaluation indicators

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

Alpha Beta Pruning in Adversarial Search

【Paper Reading】

【TCDCN】《Facial landmark detection by deep multi-task learning》
![[paper introduction] r-drop: regulated dropout for neural networks](/img/09/4755e094b789b560c6b10323ebd5c1.png)
[paper introduction] r-drop: regulated dropout for neural networks

Sorting out dialectics of nature

超时停靠视频生成

Using compose to realize visible scrollbar
随机推荐
PHP returns the corresponding key value according to the value in the two-dimensional array
常见的机器学习相关评价指标
Calculate the difference in days, months, and years between two dates in PHP
How do vision transformer work? [interpretation of the paper]
Interpretation of ernie1.0 and ernie2.0 papers
A slide with two tables will help you quickly understand the target detection
Jordan decomposition example of matrix
[model distillation] tinybert: distilling Bert for natural language understanding
【Mixup】《Mixup:Beyond Empirical Risk Minimization》
Network metering - transport layer
【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》
Use Baidu network disk to upload data to the server
[CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
Huawei machine test questions
(15) Flick custom source
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
[introduction to information retrieval] Chapter 1 Boolean retrieval
Pointnet understanding (step 4 of pointnet Implementation)
【Random Erasing】《Random Erasing Data Augmentation》
How to clean up logs on notebook computers to improve the response speed of web pages