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
I finally managed to get it to work here is my code:
try to use this code:
With this, you can sure that the mandatories data are provided.