MariaDB connection in C#.Net
How to connect MariaDB on C#.Net using MySQL Connector
MariaDB is one of the best alternative for MySQL database. C#.Net applications can use MariaDB using any MySQL connector library, I prefer MySQL connector which is all in One solution for my needs.
Setup
Add the dependency , install the following or find compatible one using Nuget Package Manager.
NuGet\Install-Package MySqlConnector -Version 2.2.5
Lets configure the connection using the MySqlConnector.
using (var con = new MySqlConnector.MySqlConnection())
{
string mariacstr= "Server=127.0.2; port=3307; database=mydb; uid=root; pwd=123;";
con.ConnectionString = mariacstr;
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
con.Insert<User>(new User() { Name = "manoj" });
var r = con.Query("select * from users");
}
}
We also executed a create table logic. For ease of use I prefer to use ORM (dapper) for easy data handling.
For more information on Dapper refer the Sqlite example.