I'm trying to re-write an existing Java code in C#. Database is an AWS RDS MySQL instance and it already works with Java. This is the working Java code for JDBC,
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;
I'm using the following code with C# console application,
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");
}
When I run the app I get the following error,
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)
I rechecked the instance name and the SQL server is publicly accessible in AWS.
Are there any other parameters required for the connection to succeed? TIA