How to properly connect MySQL database with C# application? How to properly connect MySQL database with C# application? azure azure

How to properly connect MySQL database with C# application?


(using password: NO) says that no password was provided. I am not familiar with the connection syntax for C#, but I would suspect that something is wrong with

static string connstring =     @"server=my.live.ip;userid=user;password=123;database=db_name;port=3306";

Or perhaps connstring is not being used.

Also check the MySQL side. Do SHOW GRANTS FOR 'user'@'202%' or maybe SHOW GRANTS FOR 'user'@'202.xxx.xxx.xxx', depending on whether you used '202%' or '202.xxx.xxx.xxx' as the "host".

You should get back something like

GRANT ... ON dbname.* TO 'user'@'202%' WITH AUTHENTICATION 'mysql_native_password';

Note: having a hostname versus an IP address may be an issue.


Couple of possible things to try -

  1. Match your server name in the connection string against what is set on Azure portal.
  2. Looks like the username and password you mentioned are dummy ones (understandably so). Check if your actual user name or password have any special characters, you will have to encode them appropriately to escape.

For example, if your password is my>Pass, connection string should look like this.static string connstring = @"server=servername;userid=user;password=my>Pass;database=db_name;port=3306";

Here is a full list if you would like to explore.


using System;using System.Threading.Tasks;using MySql.Data.MySqlClient;namespace AzureMySqlExample{class MySqlCreate{    static async Task Main(string[] args)    {        var builder = new MySqlConnectionStringBuilder        {            Server = "YOUR-SERVER.mysql.database.azure.com",            Database = "YOUR-DATABASE",            UserID = "USER@YOUR-SERVER",            Password = "PASSWORD",            SslMode = MySqlSslMode.Required,        };        using (var conn = new MySqlConnection(builder.ConnectionString))        {            Console.WriteLine("Opening connection");            await conn.OpenAsync();            using (var command = conn.CreateCommand())            {                command.CommandText = "DROP TABLE IF EXISTS inventory;";                await command.ExecuteNonQueryAsync();                Console.WriteLine("Finished dropping table (if existed)");                command.CommandText = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";                await command.ExecuteNonQueryAsync();                Console.WriteLine("Finished creating table");                command.CommandText = @"INSERT INTO inventory (name, quantity) VALUES (@name1, @quantity1),                    (@name2, @quantity2), (@name3, @quantity3);";                command.Parameters.AddWithValue("@name1", "banana");                command.Parameters.AddWithValue("@quantity1", 150);                command.Parameters.AddWithValue("@name2", "orange");                command.Parameters.AddWithValue("@quantity2", 154);                command.Parameters.AddWithValue("@name3", "apple");                command.Parameters.AddWithValue("@quantity3", 100);                int rowCount = await command.ExecuteNonQueryAsync();                Console.WriteLine(String.Format("Number of rows inserted={0}", rowCount));            }            // connection will be closed by the 'using' block            Console.WriteLine("Closing connection");        }        Console.WriteLine("Press RETURN to exit");        Console.ReadLine();    }}

}