如何正确地将MySQL数据库与C#应用程序连接? [英] How to properly connect MySQL database with C# application?

查看:104
本文介绍了如何正确地将MySQL数据库与C#应用程序连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 MySQL 数据库与 C#连接,但它没有连接。

I am trying to connect my MySQL database with C# but it's not getting connected.

我正在使用

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

但我还是得到


使用方法 mysql_native_password对用户 user托管 my.live.ip的身份验证失败,并显示以下消息:用户'user'@'202.xxx.xxx.xxx'的访问被拒绝(使用密码) :否)`

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).

PS:我正在使用的实时IP 天蓝色。即 MySQL 数据库通过 xampp

P.S: The live IP that I am using is of azure. i.e. MySQL database is hosted on azure server via xampp

任何帮助将不胜感激

推荐答案

您可以直接查找,但是无论如何,这是我发现的代码:

You can just look it up but here’s come code anyway that I found:

将所有端口号用户名和密码数据库名称以及服务器ip替换为所需的内容。

Replace all port # usernames and password database name and server ip with what you need.

代码:

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.

这篇关于如何正确地将MySQL数据库与C#应用程序连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆