当前位置:网站首页>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
边栏推荐
- SVN
- Apache foundation officially announced Apache inlong as a top-level project
- .net使用Access 2010数据库
- Drag the mouse to rotate the display around an object
- Summary of common terms and knowledge in SMT chip processing industry
- 4 key points to help the product manage the project well
- Electronic training.
- Example: use C # Net to teach you how to develop wechat official account (21) -- using wechat to pay online collection: H5 method
- 2021-04-28
- Web學習之TypeScript
猜你喜欢
How product managers control the progress of product development
11.1.1 overview of Flink_ Flink overview
.net使用Access 2010数据库
Summary of push-pull output and open drain output of STM32 and failure of analog IIC driving mlx90615
debezium
Idea set the template of mapper mapping file
Ssl/tls, symmetric and asymmetric encryption, and tlsv1.3
86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
Chapter V exercises (124, 678, 15, 19, 22) [microcomputer principles] [exercises]
案例:绘制Matplotlib动态图
随机推荐
jarvisoj_level2_x64
CXF
Flink reports error: a JNI error has occurred, please check your installation and try again
PHP performance optimization
Anaconda beginner's notes
Correct writing methods of case, number and punctuation in Chinese and English papers
C IO stream (I) basic concept_ Basic definition
1-9network configuration in VMWare
ciscn_2019_en_2
Run the test program using rknn-toolkit-lite2 for rk3568 development board
案例:绘制Matplotlib动态图
统一网关Gateway
1-10Vmware构建自定义的网络架构
Reentrant functions must be used within signal processing functions
事物/现象/事情/东西/情况/表象
Solution to SMT grape ball phenomenon
C#使用MySql进行操作
Idea kotlin version upgrade
Causes and solutions to the phenomenon of PCBA monument in SMT patch processing
Typescript for Web Learning