[英]C# Winforms connection to MySQL AWS Database
我正在尝试将winforms .net应用程序与AWS RDS MySQL数据库连接,但是我很难建立连接。 我已经阅读了很多有关通过Microsoft SQL数据库和Elastic Beanstalk进行连接的资料,但是我没有找到我要找的答案...可能是因为我是菜鸟。
我研究了以下几个问题: 如何连接到MySQL数据库? 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");
我收到以下错误消息:
MySql.Data.MySqlClient.MySqlException:'无法连接到任何指定的MySQL主机。
我缺少一些简单的东西吗? 我已将数据库端点复制到database_URL位置。 我的用户名和密码正确。 我的数据库在AWS上设置为MySQL数据库。
给出错误消息是因为无法连接到主机。
在连接字符串中,将localhost作为服务器,但是您的数据库位于云(AWS)上,因此,这意味着您必须指定数据库的IP或指向该数据库的域名,而不是本地(本地意味着电脑)。 例如
string conn = "server=192.168.0.7; Database=database_name; User Id=admin; Password=myPassword";
请注意,服务器IP由AWS提供,并且您要确保端口已启用。 MySQL最常见的端口是3306。
最好的祝福。
尝试这个,
//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();
使用ConnectionStrings进行检查会使其看起来好像您的参数名错误。 “用户名”应为“ uid”,“密码”应为“ pw”。
无论如何,我建议使用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.