C ා operate mysql database

Keywords: C# Database MySQL SQL

This paper introduces the operation of adding, deleting, modifying and querying mysql database.

1. Install the database, pay attention to the installation of Connector NET.

2. Introduce MySql.Data.dll and add project reference. The general location of the file is: C:\Program Files (x86)\MySQL\Connector NET xx\Assemblies\v xx (determined by MySQL installation location).

3. Add a test table in MySQL Workbench. In this example, a database test is created, and a user table is created in the database. There are two table items in the table, username and password, as follows:

  

4. Add, delete, modify and check the database. The C code is as follows:

 1 using System;
 2 using MySql.Data.MySqlClient;
 3 
 4 namespace MySQL Database operations
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             // Database configuration
11             string connStr = "Database=test;datasource=127.0.0.1;port=3306;user=root;pwd=root;";
12             MySqlConnection conn = new MySqlConnection(connStr);
13 
14             conn.Open();
15 
16             #region query
17 //            // query user All entries in the table
18 //            MySqlCommand cmd = new MySqlCommand("select * from user", conn);
19 //
20 //            MySqlDataReader reader = cmd.ExecuteReader();
21 //
22 //            // Read data line by line
23 //            while (reader.Read())
24 //            {
25 //                string username = reader.GetString("username");
26 //                string password = reader.GetString("password");
27 //                Console.WriteLine(username + ":" + password);
28 //            }
29 //
30 //            reader.Close();
31             #endregion
32 
33             #region insert
34 //            // Insert a piece of data normally
35 //            string username = "lj";string password = "6666";
36 //            MySqlCommand cmd = new MySqlCommand("insert into user set username ='" + username + "'" + ",password='" + password + "'", conn);
37 //            cmd.ExecuteNonQuery();
38             
39 //            // sql Injection, database will be deleted
40 //            string username = "lj"; string password = "6666'; delete from user;";
41 //            MySqlCommand cmd = new MySqlCommand("insert into user set username ='" + username + "'" + ",password='" + password + "'", conn);
42 //            cmd.ExecuteNonQuery();
43 
44 //            // Prevent injection,Delete database statement will not be executed
45 //            string username = "lj"; string password = "6666'; delete from user;";
46 //            MySqlCommand cmd = new MySqlCommand("insert into user set username=@uid , password = @pwd", conn);
47 //            cmd.Parameters.AddWithValue("uid", username);
48 //            cmd.Parameters.AddWithValue("pwd", password);
49 //            cmd.ExecuteNonQuery();
50             #endregion
51 
52             #region delete
53 //            // delete id Entry for 6
54 //            MySqlCommand cmd = new MySqlCommand("delete from user where id = @id", conn);
55 //            cmd.Parameters.AddWithValue("id", 6);
56 //
57 //            cmd.ExecuteNonQuery();
58             #endregion
59 
60             #region To update
61             // take id Entry for 7 pwd Modified to lll
62             MySqlCommand cmd = new MySqlCommand("update user set password = @pwd where id = 7", conn);
63             cmd.Parameters.AddWithValue("pwd", "lll");
64 
65             cmd.ExecuteNonQuery();
66             #endregion
67 
68             conn.Close();
69 
70             Console.ReadKey();
71         }
72     }
73 }

Posted by Reviresco on Fri, 20 Mar 2020 12:15:56 -0700