MySqlConnector with MySqlCommand.ExecuteNonQueryAsync in C# not creating records

I’m porting a working backend written in Python to .NET microservice written in C#, using the MySQLConnector and mySQL. I have a simple C# Database class instantiated from Program main as one of the first lines. It connects to the mysql service, then checks for existence of DB and creates it and the tables if needed, then populates it with a few seed records to make it operational.

I’m trying to do this with proper error checking and structure and learn the right way to do things.

My problems are:

  • I’m not understanding how to structure the async code properly.
  • One of the Async calls is blocking and never returning
  • I’m not sure how to do an Async call to get a connection for a method that needs to make a query

Below is my current class, followed by my questions, and the original working Python database module below that. Any help, tips or tricks appreciated.

using System;
using System.Text.RegularExpressions;
using MySqlConnector;

// MySqlConnector notes and refs
// https://mysqlconnector.net/api/mysqlconnector/mysqlerrorcodetype/
// https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
// Adventureworks naming conventions: 
// /questions/3593582/database-naming-conventions-by-microsoft

public class Database
{
    static private Database instance;
    private bool isReady;

    private string dbName;
    private string dbUser;
    private string dbPassword;
    private string dbHost;
    private string dbPort;
    private string connectionString;

    public Database(string dbName, string dbUser, string dbPassword, string dbHost, string dbPort)
    {
        this.dbName = dbName;
        this.dbUser = dbUser;
        this.dbPassword = dbPassword;
        this.dbHost = dbHost;
        this.dbPort = dbPort;
        this.connectionString = $"server={dbHost};user={dbUser};port={dbPort};password={dbPassword}";
        instance = this;
        Initialize();
    }

    // is this the right approach for various methods in the service to get a connection?
    static public MySqlConnection Connect()
    {
        try
        {
            var connection = new MySqlConnection(instance.connectionString);
            connection.Open();

            var command = new MySqlCommand($"USE {instance.dbName}", connection);
            command.ExecuteNonQuery();

            return connection;
        }
        catch (MySqlException ex)
        {
            switch ((MySqlErrorCode)ex.Number)
            {
                case MySqlErrorCode.AccessDenied:
                    Console.WriteLine("Access denied: Something is wrong with your user name or password");
                    break;
                case MySqlErrorCode.UnknownDatabase:
                    Console.WriteLine($"Database {instance.dbName} does not exist");
                    break;
                default:
                    Console.WriteLine(ex.Message);
                    break;
            }

            Environment.Exit(1);
            return null;  // this line will never be reached, but it's necessary to satisfy the return type of the function.
        }
    }

    static public bool IsReady()
    {
        return (instance != null && instance.isReady);
    }

    //*************************************************************************
    // Private methods
    private async Task Initialize()
    {
        // create a connection and try to open the DB service
        using var connection = new MySqlConnection(connectionString);
        try
        {
            await connection.OpenAsync();
        }
        catch (MySqlException exConnection)
        {
            Console.WriteLine(exConnection.Message);
            Environment.Exit(1);
        }

        // service exists, now check if our database exists
        try
        {
            // if it got here, then it found it, need to make it active via USE
            using var command = new MySqlCommand($"USE {dbName}", connection);
            await command.ExecuteNonQueryAsync();
        }
        catch (MySqlException exUseDB)
        {
            if ((MySqlErrorCode)exUseDB.Number == MySqlErrorCode.UnknownDatabase)
            {
                Console.WriteLine($"Database {dbName} does not exist");
                Console.WriteLine($"Creating {dbName}...");
                // create the database...
                try
                {
                    // load the schema file and run the sql to create the db and tables
                    string schema = File.ReadAllText("CLIService/SQL/bbe-schema.sql");
                    string[] sqlCommands = schema.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                    // create the tables
                    foreach (string sqlCommand in sqlCommands)
                    {
                        string cleanedCommand = Regex.Replace(sqlCommand, "[nr]", "");

                        if (!string.IsNullOrEmpty(cleanedCommand))
                        {
                            try
                            {
                                var command = new MySqlCommand(cleanedCommand, connection);
                                await command.ExecuteNonQueryAsync();
                            }
                            catch (MySqlException exSqlCommand)
                            {
                                Console.WriteLine(exSqlCommand.Message);
                                Environment.Exit(1);
                            }
                        }
                    }
                }
                catch (MySqlException exCreateDB)
                {
                    Console.WriteLine($"Failed creating database: {exCreateDB.Message}");
                    Environment.Exit(1);
                }
            }
            else
            {
                Console.WriteLine(exUseDB.Message);
                Environment.Exit(1);
            }
        }

        // database exists and is used, now populate it if empty
        try
        {
            // there is always 1 admin account created by default, so try to get one record
            var command = new MySqlCommand("SELECT * FROM account LIMIT 1", connection);
            // should I use ExecuteReaderAsync() instead? If so how?
            using (var reader = command.ExecuteReader())
            {
                // if no records, means it's a new, empty database
                if (!reader.HasRows)
                {
                    // load the data file and run the sql to create the table records of initial data
                    string schema = File.ReadAllText("CLIService/SQL/bbe-data.sql");
                    string[] sqlCommands = schema.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                    // create the records
                    foreach (string sqlCommand in sqlCommands)
                    {
                        string cleanedCommand = Regex.Replace(sqlCommand, "[nr]", "");

                        if (!string.IsNullOrEmpty(cleanedCommand))
                        {
                            try
                            {
                                command = new MySqlCommand(cleanedCommand, connection);
#if USE_ASYNC_AWAIT
                                // the following line if used it hangs/blocks and never returns
                                await command.ExecuteNonQueryAsync();
#else
                                // alternatively the following line returns: "56 Inserted System.Threading.Tasks.Task`1[System.Int32] records"
                                // but no records are inserted into the DB
                                var rowCount = command.ExecuteNonQueryAsync();
                                Console.WriteLine($"Inserted {rowCount} records");
#endif
                            }
                            catch (MySqlException exSqlCommand)
                            {
                                Console.WriteLine(exSqlCommand.Message);
                                Environment.Exit(1);
                            }
                        }
                    }
                }
            }
        }
        catch (MySqlException exSelectDB)
        {
            Console.WriteLine($"An error occurred: {exSelectDB.Message}");
        }

        // database opened, selected and populated
        isReady = true;
        Console.WriteLine("Database is ready");
    }
}

Questions:

  1. What am I doing wrong in the Initialize() method for the USE_ASYNC_AWAIT section of the code? The current code successful connects to the service, detects the DB is missing and creates it and the tables using a file of sql commands, detects there are no records, loads the data file and loops through the insert commands. In the code above where is says #if USE_ASYNC_AWAIT, if USE_ASYNC_AWAIT is true then await command.ExecuteNonQueryAsync blocks and never returns, and if false it completes with 56 tasks created but no records are ever created.
  2. Is the overall structure of Initialize method the right way to do things and handle exceptions?
  3. How should the Connect function be structured using Async when services ask for a connection to run queries on?

Original working Python backend:

import mysql
from mysql.connector import errorcode
import re
import os
import tomllib


def connect(config):
    # open the database connection
    try:
        connection = mysql.connector.connect(pool_name="bbepool", **config["DATABASE"]["CONNECTION"])
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Access denied: Something is wrong with your user name or password")
        else:
            print(err)
            exit(1)
    # set the active database
    name = config["DATABASE"]["NAME"]
    connection.name = name
    cursor = connection.cursor()
    try:
        cursor.execute("USE {}".format(name))
    except mysql.connector.Error as err:
        print(err)
        exit(1)

    return connection


def initialize(config):
    connection = None
    cursor = None

    # open the database connection
    try:
        connection = mysql.connector.connect(**config["DATABASE"]["CONNECTION"])
        cursor = connection.cursor()
    except mysql.connector.Error as err:
        print(err)
        exit(1)

    # connection successful, check if the db exists already
    name = config["DATABASE"]["NAME"]
    try:
        cursor.execute("USE {}".format(name))
    except mysql.connector.Error as err:
        print("Database {} does not exist".format(name))
        if err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Creating {}...".format(name))
            try:
                cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8mb4'".format(name))
            except mysql.connector.Error as err:
                print("Failed creating database: {}".format(err))
                exit(1)

            print("Database {} created successfully".format(name))
            # set the active database
            connection.database = name
        else:
            print(err)
            exit(1)

    # also set connection to use the new database
    config["DATABASE"]["CONNECTION"]["database"] = config["DATABASE"]["NAME"]

    # load and run schema
    # safe to do as it doesn't create tables if they exist
    with open(config["DATABASE"]["SCHEMA"], "rb") as f:
        tables = f.read().decode("utf-8").split(";")
        for table in tables:
            table = re.sub("[nr]", "", table)
            if len(table) > 0:
                try:
                    cursor.execute(table)
                    connection.commit()
                except mysql.connector.Error as err:
                    print(err)
                    exit(1)

    # if there is test data
    if config["DATABASE"]["DATA"] != "":
        # if the db is empty
        cursor.execute("SELECT * FROM account LIMIT 1")
        # must fetch the data for the cursor
        cursor.fetchone()
        if cursor.rowcount < 1:
            # populate
            with open(config["DATABASE"]["DATA"], "rb") as f:
                text = f.read().decode("utf-8")
                lines = text.split(");n")
                for line in lines:
                    if len(line) > 0:
                        try:
                            cursor.execute(line + ");")
                            connection.commit()
                        except mysql.connector.Error as err:
                            print(err)
                            exit(1)

    # cleanup
    cursor.close()
    connection.close()


if __name__ == "__main__":
    # ensure the current working directory is same as script
    os.chdir(os.path.dirname(__file__))

    # module vars we use multiple places
    config = {}

    # In VS Code we can test development or production by setting
    # "DEPLOYMENT_ENVIRONMENT": "development", "testing" or "production" in .vscode/launch.json
    #  but in case the environment variable isn't set, make a default to development
    if "DEPLOYMENT_ENVIRONMENT" not in os.environ:
        os.environ["DEPLOYMENT_ENVIRONMENT"] = "development"
    config_file = "../Settings/config." + os.environ["DEPLOYMENT_ENVIRONMENT"] + ".toml"
    with open(config_file, "rb") as f:
        config = tomllib.load(f)

    # initialize database before switching directories
    initialize(config)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật