简体   繁体   English

C#Winforms与MySQL AWS数据库的连接

[英]C# Winforms connection to MySQL AWS Database

I'm trying to connect a winforms .net application with an AWS RDS MySQL database but I am having difficulty making the connection. 我正在尝试将winforms .net应用程序与AWS RDS MySQL数据库连接,但是我很难建立连接。 I have read a lot of material about connecting through Microsoft SQL database and through Elastic Beanstalk but I haven't come across the answer I'm looking for... possibly because I'm a noob. 我已经阅读了很多有关通过Microsoft SQL数据库和Elastic Beanstalk进行连接的资料,但是我没有找到我要找的答案...可能是因为我是菜鸟。

I've looked through a few of these questions: How to connect to MySQL Database? 我研究了以下几个问题: 如何连接到MySQL数据库? https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlConnection.htm https://dev.mysql.com/doc/dev/connector-net/8.0/html/T_MySql_Data_MySqlClient_MySqlConnection.htm

using MySql.Data.MySqlClient;

string connection = "server=localhost; Database=database_URL; User Id=admin; 
Password=myPassword";
myConn.Open();
MessageBox.Show("Success");

I'm getting the following error message: 我收到以下错误消息:

MySql.Data.MySqlClient.MySqlException: 'Unable to connect to any of the specified MySQL hosts.' MySql.Data.MySqlClient.MySqlException:'无法连接到任何指定的MySQL主机。

Is there something simple that I'm missing? 我缺少一些简单的东西吗? I have copied the database endpoint into the database_URL location. 我已将数据库端点复制到database_URL位置。 My user id and password are correct. 我的用户名和密码正确。 My database is setup on AWS as a MySQL database. 我的数据库在AWS上设置为MySQL数据库。

The error message is given because can't connect to the host. 给出错误消息是因为无法连接到主机。

In your connection string is given the localhost as the server but your database is on cloud (AWS), so it means that you must specify the database's IP or the domain name pointing to that database, not the local (local means that is in your computer). 在连接字符串中,将localhost作为服务器,但是您的数据库位于云(AWS)上,因此,这意味着您必须指定数据库的IP或指向该数据库的域名,而不是本地(本地意味着电脑)。 eg 例如

string conn = "server=192.168.0.7; Database=database_name; User Id=admin; Password=myPassword";

Note that the server IP is provided by AWS, and you'd make sure that ports are enable. 请注意,服务器IP由AWS提供,并且您要确保端口已启用。 The most common port for MySQL is 3306. MySQL最常见的端口是3306。

Best regards. 最好的祝福。

Try this, 尝试这个,

   //This is my connection string i have assigned the database file address path  
   string MyConnection2 = 
   "host='localhost';database='databasename';username='myusername';password='mypassword'";
   //This is my insert query in which i am taking input from the user through windows forms  
   string Query = "Your query";
   //This is  MySqlConnection here i have created the object and pass my connection string.  
   MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
   //This is command class which will handle the query and connection object.  
   MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
   MySqlDataReader MyReader2;
   MyConn2.Open();
   MyReader2 = MyCommand2.ExecuteReader();     
   // Here our query will be executed and data saved into the database.  
   MessageBox.Show("Save Data");
   while (MyReader2.Read())
   {
   }
   MyConn2.Close();

Checking back with ConnectionStrings makes it appear as if your parameter-names are wrong. 使用ConnectionStrings进行检查会使其看起来好像您的参数名错误。 'username' should be 'uid' and 'password' should be 'pw'. “用户名”应为“ uid”,“密码”应为“ pw”。

In any case I'd suggest using the MySqlConnectionStringBuilder-class to construct your connection string. 无论如何,我建议使用MySqlConnectionStringBuilder-class构造您的连接字符串。

var connectionStringBuilder = new MySqlConnectionStringBuilder
{
    Server = "<Instance_Ip>",
    UserID = "root",
    Password = "<Password>",
    Database = "<Database_Name>"
};

using (var conn = new MySqlConnection(connectionStringBuilder.ToString()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM