tpaiDev
tpaiDev

Reputation: 15

How do I connect and send queries to the MariaDB database through a C# Windows application?

I'm looking for a secure way to connect and send queries to my remote sql database.

An important clause is that I don’t want to use an external directory. I want to do all this stuff with the built-in tools of .NET

I stuck because I don't know what is the safest and easiest way to do that.

I tried this How to connect MySQL database to C# WinForm Application?, but because of the old framework I can't add a mysql reference. Wondering is there a way to create a secure connection just by using:

using System.Data.SqlClient;

I'm certain the details of the server has given are correct in the connectionstring.

private void button1_Click(object sender, EventArgs e)
 {
        var conn = new SqlConnection();
        conn.ConnectionString =
                                      "Data Source=xxx.xxx.xxx.xxx,3306;" +
                                      "Initial Catalog=dbname;" +
                                      "User Id=dbuser;" +
                                      "Password=password;";
        try
        {
            conn.Open();
            MessageBox.Show("Connection Open ! ");
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! ");
        }
}

Upvotes: 1

Views: 4638

Answers (1)

Bradley Grainger
Bradley Grainger

Reputation: 28207

You can't use System.Data.SqlClient to connect to MySQL or MariaDB; it's only for Microsoft SQL Server.

To connect to MariaDB, you will need to install a third-party library. The one with the best support for MariaDB is MySqlConnector, but it requires .NET 4.5 or later.

It's possible that an old version of MySql.Data, e.g., https://www.nuget.org/packages/MySql.Data/6.8.8, still supports .NET 4.0. You should try to use the latest version you can find that still works on .NET 4.0 (in order to make sure you have the latest bug fixes).

Upvotes: 1

Related Questions