C# 无法从.NET连接器连接到Azure MySQL数据库
我尝试使用MySQL工作台和MySQL外壳连接到Azure MySQL数据库,效果很好。现在,我尝试使用以下C代码进行连接: 在这里,我用数据库密码替换了C# 无法从.NET连接器连接到Azure MySQL数据库,c#,mysql,azure,C#,Mysql,Azure,我尝试使用MySQL工作台和MySQL外壳连接到Azure MySQL数据库,效果很好。现在,我尝试使用以下C代码进行连接: 在这里,我用数据库密码替换了{my_password},它在Open方法中给了我一个异常: MySql.Data.MySqlClient.MySqlException:'主机身份验证 用户的“creator db.mysql.database.azure.com” “创造者”_main@creatur-db“使用方法”mysql\u native\u password”
{my_password}
,它在Open方法中给了我一个异常:
MySql.Data.MySqlClient.MySqlException:'主机身份验证
用户的“creator db.mysql.database.azure.com”
“创造者”_main@creatur-db“使用方法”mysql\u native\u password”
失败,消息为:连接字符串可能不正确。请
访问门户以获取参考
我还尝试了不同的连接字符串:
Server=creatur-db.mysql.database.azure.com; Port=3306; Database=test; Uid=creatur_db_main@creatur-db; Pwd={my_password}; SslMode=Preferred
及
但没有一个成功
在Visual Studio 2013中使用服务器资源管理器创建新连接时,也会发生相同的异常。这个错误似乎与.NET连接器有关。我尝试使用MySQL.Data.dll、.NET Framework和Visual Studio的不同版本,但没有成功。我刚刚使用我使用的VS2017创建了一个控制台应用程序 Nuget包MySql.Data和.NETFramework 4.6.1 我所做的一切在这里非常有效 创建MySQL服务器后,我使用CloudShell连接到服务器(而不是数据库) 使用此代码:
mysql --host franktest.mysql.database.azure.com --user frank@franktest -p
我犯了个错误
ERROR 9000 (HY000): Client with IP address '40.76.202.47' is not allowed to connect to this MySQL server.
因此,我添加了该IP,同时从连接的位置添加我的IP
所以一旦IP被保存。我执行了上一个命令,这次它运行得非常好。我使用以下命令创建了一个名为:frankdemo的数据库:
CREATE DATABASE frankdemo;
然后,在VisualStudio中,我将此代码作为主要方法,从文档中复制
static void Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "franktest.mysql.database.azure.com",
Database = "frankdemo",
UserID = "frank@franktest",
Password = "gr3enRay14!",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = "DROP TABLE IF EXISTS inventory;";
command.ExecuteNonQuery();
Console.WriteLine("Finished dropping table (if existed)");
command.CommandText = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";
command.ExecuteNonQuery();
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 = command.ExecuteNonQuery();
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();
}
运行它,它的工作
我所指的文档是:
连接字符串可能不正确。
异常,用户标识格式似乎不正确。但根据您的代码和用户ID,它是正确的格式。所以这很奇怪。我也在我这边尝试你的代码,它工作正常。我使用VS 2017、net framework 4.6.1.和库。@n-necropoliess它是PowerBi的,MySql是它的第一天。。。现在它起作用了。尝试更新你的参考资料。我给你写了一个样本。谢谢你的回答。不幸的是,它对我仍然不起作用。打开连接时也会出现相同的异常。@N.Necropoliess您可以尝试从云Shell连接到MySQL服务器吗?就像这里解释的那样:我成功地从云Shell连接到MySQL服务器,但我的代码仍然无法工作。看起来我从NuGet(版本6.10.6)获得的MySQL.Data.dll有问题。如果我用MySqlConnector替换它,那么它可以正常工作,但不幸的是我不能用NHibernate使用它,所以我需要一些方法使MySQL.Data.dll正常工作。>如果我用MySqlConnector替换它,那么它可以正常工作,但不幸的是我不能用NHibernate@N.necropoliss使用它,有什么问题阻止MySqlConnector与NHibernate一起工作?
CREATE DATABASE frankdemo;
static void Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "franktest.mysql.database.azure.com",
Database = "frankdemo",
UserID = "frank@franktest",
Password = "gr3enRay14!",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = "DROP TABLE IF EXISTS inventory;";
command.ExecuteNonQuery();
Console.WriteLine("Finished dropping table (if existed)");
command.CommandText = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";
command.ExecuteNonQuery();
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 = command.ExecuteNonQuery();
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();
}