Search code examples
c#mysqlapachexamppmariadb

C# using MySql and MariaDB


I'm an IT student from Switzerland and have some experience in C# and Java. Now in school, we have some lessons about MySql, using the XAMPP-package. I had the idea, to make a tool in C# using MySql to use Databases on MariaDB. I've searched many times in Google and on Stack Overflow, but I didn't find any solution, that works. Using Linq isn't also the same thing, so I hoped, you may help me.

string connection = "server=localhost;database=example_bankdb;uid=root;password=";
var dbConn = new MySql.Data.MySqlClient.MySqlConnection(connection);

try
{
    dbConn.Open();

    string s0 = "CREATE DATABASE IF NOT EXISTS `mydb`;";
    var cmd = new MySqlCommand(s0, dbConn);
    dbConn.Close();
    Console.WriteLine("Verbindung wird hergestellt");
}
catch
{
    Console.WriteLine("Die Verbindung kann nicht hergestellt werden");
}

Return of the Console, that the code run and should create a new database

Return of the Console, that the code run and should create a new database

I've opened the database overview and saw, that the database wasn't created

I've opened the database overview and saw, that the database wasn't created

So, here is my question, is it possible, to create and use a database from MariaDB for a C# project? And how can I manage this?

I hope, my question is formatted right and I gave enough information to solve this problem...


Solution

  • It doesn't look like you're executing the query. Try adding cmd.ExecuteNonQuery();

    dbConn.Open();
    s0 = "CREATE DATABASE IF NOT EXISTS `mydb`;";
    cmd = new MySqlCommand(s0, dbConn);
    cmd.ExecuteNonQuery();
    dbConn.Close();