如何将MySQL数据库与C#应用程序正确连接?



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

我正在使用

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

但我仍然得到

使用方法"mysql_native_password"对用户"用户"的主机"my.live.ip"进行身份验证失败,并显示消息:用户"user"@'202.xxx.xxx.xxx'的访问被拒绝(使用密码:NO(">

我已经搜索了它,但没有找到任何合适的解决方案。

PS:我正在使用的live IPazure的,即MySQL数据库通过xampp托管在 Azure 服务器上

任何帮助将不胜感激

(using password: NO)说没有提供密码。 我不熟悉 C# 的连接语法,但我怀疑有问题

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

或者也许connstring没有被使用。

还要检查 MySQL 端。 做SHOW GRANTS FOR 'user'@'202%'或可能SHOW GRANTS FOR 'user'@'202.xxx.xxx.xxx',这取决于您是使用'202%'还是'202.xxx.xxx.xxx'作为"主机"。

你应该得到类似的东西

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

注意:主机名与 IP 地址可能是一个问题。

尝试一些可能的事情 -

  1. 将连接字符串中的服务器名称与 Azure 门户上设置的名称相匹配。
  2. 看起来您提到的用户名和密码是虚拟的(可以理解(。检查您的实际用户名或密码是否有任何特殊字符,您必须对它们进行适当的编码才能转义。

例如,如果密码为 my>Pass,则连接字符串应如下所示。static string connstring = @"server=servername;userid=user;password=my>Pass;database=db_name;port=3306";

如果您想探索,这里有一个完整的列表。

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();
}
}

}

你可以查一下,但无论如何我找到了代码:

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

法典:

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();
}
}
}

我希望这段代码对你有用。

看起来您给我的错误也表明用户名或密码无效。

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();
}
}
}

您可以参考下面的链接使用 C# 创建和访问 MySQL 数据库 https://learn.microsoft.com/en-us/azure/mysql/connect-csharp

您的密码是否以#或类似的字符开头,并且您将其存储在某个配置文件中,例如ini

由于某些配置类型将某些特殊字符视为注释或其他字符,因此密码变为null

要进行验证,请将密码更改为字母数字一次并进行测试。当我使用时,我的密码面临这个问题password=#strongP

试试这个:

const string connStr = "Server=my.live.ip; User=user; Database=db_name; Password=123; port=3306; 
Allow Zero Datetime=true; Convert Zero Datetime=true;"; 

最新更新