开发应用程序连接mysql数据库,如何正确地将MySQL数据库与C#应用程序连接?

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值