当前位置:网站首页>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

原网站

版权声明
本文为[App Hunter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252244286938.html