I am trying to connect my MySQL database with C# but it's not getting connected.
I am using
static string connstring = @"server=my.live.ip;userid=user;password=123;database=db_name;port=3306";
but still I am getting
Authentication to host 'my.live.ip' for user 'user' using method 'mysql_native_password' failed with message: Access denied for user 'user'@'202.xxx.xxx.xxx' (using password: NO)`
I have searched on it but didn't find any suitable solution(s).
P.S: The live IP that I am using is of azure. i.e. MySQL database is hosted on azure server via xampp
Any help would be highly appreciated
解决方案
You can just look it up but here’s come code anyway that I found:
Replace all port # usernames and password database name and server ip with what you need.
Code:
using System;
using System.Windows;
using MySql.Data.MySqlClient;
namespace Deportes_WPF
{
public partial class Login : Window
{
private MySqlConnection connection;
private string server;
private string database;
private string user;
private string password;
private string port;
private string connectionString;
private string sslM;
public Login()
{
InitializeComponent();
server = "server_name";
database = "database_name";
user = "user_id";
password = "password";
port = "3306";
sslM = "none";
connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);
connection = new MySqlConnection(connectionString);
}
private void conexion()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
conexion();
}
}
}
I hope this code works for you.
It also looks like for the error you gave me that the username or password is invalid.