[C#] Using MySQL with C# –1 Visual Studio 2019 + MySQL 5.7 Connectors Installation

4 minute read

Premises and preparation

C # article

Recently, I have been privately creating a website that many people can feel through nature and history through single-lens reflex photographs and small attractions on the Miura Peninsula.jp/yokosuka/photo_001/),Lollipop!IamusingPHP+MySQLfortherentalserver(*´꒳`*)

Regarding the data registration of MySQL, if you can register MySQL from PHP on the Internet, even if you have to create an account for registration on the PHP side or you need to strengthen security such as login, it is still the Internet Since there is a limit as long as it is above, ** I registered the data locally in MySQL and applied the generated SQL patch to the operating server **. In that case, there is no need to create a management screen on the Internet, and although it is a little better, you have to create a MySQL data registration program locally.

So, in my case, I use the method ** to create a Windows program in C #, connect to MySQL from there, and generate a SQL patch as text **. In that case, it is absolutely necessary to use an external library for ** connecting to MySQL from C # (.NET) **, and since it is not provided as standard in Visual Studio, it is necessary to prepare a third party connector. It was there, so I installed it (˶ ・ ᴗ ・) ੭

Environment

  • -OS: Windows 10 Pro (using Windows sandbox to reproduce the installation)
  • -Development environment: Visual Studio 2019
  • -Database: MySQL 5.7

Premise

nothing special

Work procedure

This time, I will install it on the development environment side. Since MySQL was already available in C # in my main environment, I decided to reproduce the installation in the Windows sandbox (\ * ॑ ꒳ ॑ \ *) ⋆ \ *

Install Visual Studio and MySQL

Installing Visual Studio 2019

Visual Studio seems to have a short cycle period of 2015 → 2017 → 2019… in a short period of time, but I think that other versions of Visual Studio can be used, but I am using 2019

Visual Studio 2019

I installed it by default as it is (˙꒳ ˙ᐢ) By the way, all items that use C # are installed.

Install MySQL

I used MySQL 8.0 for server construction on Linux, but I used 5.7 because it was 5.7 for Windows.

MySQL 5.7

** Select “Developer Default” ** to install. By default for development, ** “MySQL Connector” ** is picked up as an item like the screen, and it supports Java, .NET, C / C ++, etc.

Use with Visual Studio

Try adding a reference

Start Visual Studio and create a new C # form application project. Then, there is “Reference” in Solution Explorer, so add the reference and open “Reference Manager”.

MySql.Data

Since it is actually automatically recognized in “Assembly”, if you enter “mysql” in the search screen on the upper right in the reference manager, “** MySql.Data **” will appear in many lines as shown in the screen above. Since it will appear, for the time being, select only one, any one (turn on the check box that appears when you hover the mouse to the left of the line) and select “OK” …

Use MySQL

“** MySql.Data **” has been added (\ * ˘꒳˘ \ *) It contains a C # object that handles MySQL.

Describe in source

To actually use it in the source, it was convenient to specify it with “using” first, so I did that

sample1.cs


using System;
using System.text;
 (Omitted)

using MySql.Data.MySqlClient; // This contains an object to work with MySQL

namespace WindowsForms1
{
    class sample1
    {
        public void sampleMethod ()
            try {
                // Give connection information as a string
                string connStr = "server = 127.0.0.1; user id = test; password = test1; database = manutest";
                MySqlConnection conn = new MySqlConnection (connStr);
                
                // open a connection
                conn.Open ();
                
                // Write SQL processing here
                
                // close the connection
                conn.Close ();
            }
            catch (MySqlException mse)
            {
                // Write MySql connection error handling here
            }
        }
    }
}

I wrote the source in this way, and since there was no official basis for C # of MySQL Connectors, I had to rely on third party information and topics that actually popped up in Visual Studio coding. It was … (´ • ̥ ̫ • ̥ `)

using

MySQL connection object was recognized

Actually there was no error and I grasped the basic coding flow (˶ ・ ᴗ ・) ੭⚐⚑

next time

Now that I have introduced MySQL to Visual Studio, I plan to actually connect to MySQL and summarize the data exchange in the article ٩ (. ›‹. ♡) ۶

References

  1. Let’s use MySQL Connector / NET
  2. Connect to MySQL from C # (https://dianxnao.com/c%E3%81%8B%E3%82%89mysql%E3%81%AB%E6%8E%A5%E7%B6%9A % E3% 81% 99% E3% 82% 8B /)-Cyber products
  3. [Visual Studio] How to use MySql from C # How to connect? How to execute a SQL statement? –I was in trouble
  4. I want to retrieve the result of SELECT from MySQL with C #