skip to Main Content

I can’t connect to my MySQL server with my CLI!

Im trying to connect to my mysql database using c# but i get Object cannot be cast from DBNull to other types. for no reason!

using MythicalDash;
using MySql.Data.MySqlClient;

namespace MythicalDash
{
    public class Database {
        FileManager fm = new FileManager();
        
        public void Configurator() {
            Program.logger.Log(LogType.Info, "Hi, please fill in your database configuration for MythicalDash.");
            Console.Write("Host: ");
            #pragma warning disable
            string host = Console.ReadLine();
            Console.Write("Port: ");
            string port = Console.ReadLine();
            Console.Write("Username: ");
            string username = Console.ReadLine();
            Console.Write("Password: ");
            string password = Console.ReadLine();
            Console.Write("Database Name: ");
            string dbName = Console.ReadLine();
            #pragma warning restore
            string connectionString = "Server="+host+";Port="+port+";Database="+dbName+";Uid="+username+";Pwd="+password+";";
            try
            {
                var conn = new MySqlConnection(connectionString);
                conn.Open();
                Program.logger.Log(LogType.Info,"Done");
                Environment.Exit(0x0);
            }
            catch (Exception ex)
            {
                Program.logger.Log(LogType.Error,$"Failed to connect to MySQL: {ex.Message}");
                Environment.Exit(0x0);
            }
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I finally managed to get it to work here is my code:

    using MythicalDash;
    using MySqlConnector;
    namespace MythicalDash
    {
        public class Database
        {
            FileManager fm = new FileManager();
            public void Configurator()
            {
                #pragma warning disable
                Program.logger.Log(LogType.Info, "Hi, please fill in your database configuration for MythicalDash.");
                Console.Write("Host: ");
                string host = Console.ReadLine();
                Console.Write("Port: ");
                string port = Console.ReadLine();
                Console.Write("Username: ");
                string username = Console.ReadLine();
                Console.Write("Password: ");
                string password = Console.ReadLine();
                Console.Write("Database Name: ");
                string dbName = Console.ReadLine();
                #pragma warning restore
                // Validate input
                if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(dbName))
                {
                    Program.logger.Log(LogType.Error, "Invalid input. Please provide all the required values.");
                    Environment.Exit(0x0);
                }
                try
                {
                    using var connection = new MySqlConnection($"Server={host};Port={port};User ID={username};Password={password};Database={dbName}");
                    connection.Open();
                    Program.logger.Log(LogType.Info, "Connected to MySQL, saving database configuration to config.");
                    connection.Close();
                    
                }
                catch (Exception ex)
                {
                    Program.logger.Log(LogType.Error, $"Failed to connect to MySQL: {ex.Message}");
                    Environment.Exit(0x0);
                }
            }
        }
    }
    
    

  2. try to use this code:

    public void Configurator() {
        Program.logger.Log(LogType.Info, "Hi, please fill in your database configuration for MythicalDash.");
        Console.Write("Host: ");
        string host = Console.ReadLine();
        Console.Write("Port: ");
        string port = Console.ReadLine();
        Console.Write("Username: ");
        string username = Console.ReadLine();
        Console.Write("Password: ");
        string password = Console.ReadLine();
        Console.Write("Database Name: ");
        string dbName = Console.ReadLine();
        
        // Validate input
        if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(dbName)) {
            Program.logger.Log(LogType.Error, "Invalid input. Please provide all the required values.");
            Environment.Exit(0x1);
        }
    
        string connectionString = "Server=" + host + ";Port=" + port + ";Database=" + dbName + ";Uid=" + username + ";Pwd=" + password + ";";
        try
        {
            var conn = new MySqlConnection(connectionString);
            conn.Open();
            Program.logger.Log(LogType.Info, "Connected to MySQL successfully.");
            Environment.Exit(0x0);
        }
        catch (Exception ex)
        {
            Program.logger.Log(LogType.Error, $"Failed to connect to MySQL: {ex.Message}");
            Environment.Exit(0x1);
        }
    }
    

    With this, you can sure that the mandatories data are provided.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search