当前位置:网站首页>C # operate with MySQL
C # operate with MySQL
2022-06-26 00:53:00 【App Hunter】
1. install Mysql( Online tutorials include )
2. Create project webapi
3. quote dll Generally in ( install MySQL Some have no reference :ASP.NET Connect MySQL database The detailed steps - wusir - Blog Garden ):C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.23\Assemblies\v4.5.2\MySql.Data.dll

4. Code :( Connect -》 Inquire about -》 Format get MySql The data of )
public static void GetMySqlData()
{
MySqlConnection conn = null;// establish MySqlConnection class
conn = new MySqlConnection("server=localhost;user id=root;password=123456;database=magessystemdb; pooling=true;");// Create connection string , Provide the necessary information to connect to the database
conn.Open();// Connect to database
if (conn.State.ToString() == "Open")// Check whether the connection is successful
{
Console.WriteLine(" Connect MySQL Database success ");
//MySqlCommand cmd = new MySqlCommand("insert into use values(0,'hubuyu')", conn);// establish MySqlCommand class , And provide SQL sentence , And what to do MySqlConnection object
//int n = cmd.ExecuteNonQuery();// perform SQL sentence , Operate on the connected database ,ExecuteNonQuery() Method cannot be used in a query statement
// Console.WriteLine(" Number of rows affected :" + n);// Check the return value , Is the number of rows affected
MySqlCommand cmd = new MySqlCommand("select*from `use`", conn);
int n = cmd.ExecuteNonQuery();
Console.WriteLine(" Select a query method :1 For the use of MySqlDataReader Class to query ,2 For the use of ADO.NET(MySqlDataAdapter,DataSet,DataTable class ) The query :");
int s = 2;
if (s == 1)
{
MySqlDataReader dr;// If you want to use a query statement , Should be created first MySqlDataReader class , This is a ExecuteReader() Return type of method
dr = cmd.ExecuteReader();// Use ExecuteReader() Method to execute the query statement
while (dr.Read())// Use Read() Method to query the returned MySqlDataReader Data in object , The return value is true or false
{
Console.WriteLine(dr["Id"] + " " + dr["Name"]);// Output the queried data ,dr[" Field name "], The field name here corresponds to the field name in the table queried by the database
}
dr.Close();// The key step , Don't forget to close MySqlDataReader object !!!!!
}
else if (s == 2)
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);// The second query method , Use MySqlDataAdapter object
DataSet ds = new DataSet();// establish DataSet object
DataTable dt = new DataTable();// establish DataTable object
da.Fill(ds, "use");// call Fill Method execution SQL sentence , And save the query in DataSet In the object
dt = ds.Tables["use"];// After retrieving the database , The retrieved data is stored in DataTable In the object
for (int i = 0; i < dt.Rows.Count; i++)// Reading data
{
var Id = dt.Rows[i]["Id"].ToString();
var Name = dt.Rows[i]["Name"].ToString();
Console.WriteLine(dt.Rows[i]["Id"].ToString() + " " + dt.Rows[i]["Name"].ToString());
}
}
}
else
{
Console.WriteLine(" The connection fails ");
}
conn.Close();// The key step , Don't forget to close the connection !!!!!
}
5. It can be combined in the later stage SQL Statements are dynamically generated and mapped into entity classes
6. In this way, you can operate MySql Database
7. Pit encountered :MySql.Data The version of should correspond to the corresponding .net Framework Corresponding :Mysql 8.0.23 Corresponding .net 4.7.2
8.MySql Tools :https://download.csdn.net/download/weixin_42401291/15384716
9:vs Code :MySqlManageSystem.zip-MySQL Document resources -CSDN download
---------------------------20220506-------------------------------
Reunderstanding
EF Core Database management
1. Create a database by instruction or update , Delete data table
EF Core Tool reference ( Package manager console )- EF Core | Microsoft Docs
边栏推荐
- C IO stream (II) extension class_ Packer
- leetcode. 14 --- longest public prefix
- Anaconda beginner's notes
- Idea view unit test coverage
- react + router 框架下的路由跳转后缓存页面存初始参数
- Understanding of prototypes and prototype chains
- Qt优秀开源项目之九:qTox
- Things / phenomena / things / things / situations / appearances
- Idea set the template of mapper mapping file
- 关于EF翻页查询数据库
猜你喜欢

DPVS fullnat mode management

Balanced binary tree AVL

Cloud rendering and Intel jointly create the "core" era of cloud rendering

学习识别对话式问答中的后续问题

Preorder and middle order traversal of forest

Web學習之TypeScript

1-9network configuration in VMWare

Idea kotlin version upgrade

Camkiia et gcamp6f sont - ils les mêmes?

Installation and configuration of gradle environment
随机推荐
213. house raiding II
Methods of modifying elements in JS array
Flink报错:Error: A JNI error has occurred, please check your installation and try again
1-11Vmware虚拟机常见的问题解决
使用VS2022編譯Telegram桌面端(tdesktop)
Middle order clue binary tree
【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
Methods to realize asynchrony
Balanced binary tree AVL
Idea view unit test coverage
随便画画的
DPVS fullnat mode kept
Correct writing methods of case, number and punctuation in Chinese and English papers
Is camkiia the same as gcamp6f?
Optimized three-dimensional space positioning method and its fast implementation in C language
ciscn_2019_en_2
C#线程池控制Semaphore
Phoenix index
学习识别对话式问答中的后续问题
Penetration tool -burpsuite