AWS RDS使用C#连接到MySQL数据库

时间:2018-07-28 10:40:31

标签: c# mysql amazon-rds

我试图用C#重写现有的Java代码。 数据库是一个AWS RDS MySQL实例,它已经可以与Java一起使用。 这是JDBC的有效Java代码,

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";   
static final String HOST_NAME = "DB_INSTANCE.chuogf2xvc5f.ap-south-1.rds.amazonaws.com";
static final String PORT = "3306";
static final String USER = "username";
static final String PASS = "password";
static final String DB_URL = "jdbc:mysql://" + HOST_NAME + ":" + PORT + "/DB_INSTANCE?user=" + USER + "&password=" + PASS;

我在C#控制台应用程序中使用以下代码,

string dbname = appConfig["RDS_DB_NAME"];
string username = appConfig["RDS_USERNAME"];
string password = appConfig["RDS_PASSWORD"];
string hostname = appConfig["RDS_HOSTNAME"];
string port = appConfig["RDS_PORT"];

string connectionString = "Data Source=" + hostname + ";Initial Catalog=" + dbname + ";User ID=" + username + ";Password=" + password + ";";

using (SqlConnection connection = new SqlConnection(connectionString))
  {
      Console.WriteLine(connection.ConnectionString);
      connection.Open();
      Console.WriteLine("Success");
  }

运行该应用程序时,出现以下错误,

System.Data.SqlClient.SqlException
HResult=0x80131904
Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

我重新检查了实例名称,并且SQL Server可在AWS中公开访问。

连接成功需要其他参数吗? TIA

0 个答案:

没有答案