Connecting to AWS RDS via C# - c#

I am trying to connect to MySQL AWS RDS using following simple C# code.
string cs = #"server=swift-test.czjkayncnfz9.us-east-2.rds.amazonaws.com;port=3306;userid=*****;password=*****;database=swift-db";
var con = new MySqlConnection(cs);
con.Open();
But I get the following exception
"Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time
, or established connection failed because connected host has failed to respond."
The screenshots of my Inbound & Outbound rules are attached.

Do you have any EC2 instance in this setup?
If yes, then try referencing that security group in your inbound rules with the type as MySql/Aurora.

Related

I'm able to connect to my Postgres db with PGAdmin but not with npgsql (C#)

I'm able to connect to my remote server (Postgres), and my user is able to query tables.
When I'm trying to implement connection to this db I'm getting this error:
Unhandled exception. Npgsql.PostgresException (0x80004005): 28000: no pg_hba.conf entry for host "XX.XX.XX.XX", user "XXX", database "XXX", no encryption
using (var connection = new NpgsqlConnection(connectionString))
{
connection.Open();
Error is thrown in the open, I've tried many kind of connection string:
"Server=XXX;Port=5432;Timeout=1024;CommandTimeout=1024;UserId=XX;Database=XX;Password=XX;Database=XX"
"Host=XXX;Port=5432;Username=XX;Password=XX;Database=XX"
Same result at all the attempts.
I've asked to server owner to check pg_hba.conf, waiting for his response...
but I don't understand why PGAdmin is able to connect and not my C# NPG connection :/
Thanks
I need to connect to this remote postgres db.

:'Unable to connect to any of the specified MySQL hosts.' error

I want to connect to a MySQl db using a connection string but unable to connect
I'm using UWP and using The fall creator version for both target and min version
I've installed the Nuget packages for Mysql connector
I've tried to change the string with and without port,ect.
public string GetMatchCode()
{
string connectString = "Server=###.###.###.###;Database=Db;Uid=root;Pwd=123;sslmode=none;port=3306";
string sql = "SELECT * FROM `customer`";
using (var connect = new MySqlConnection(connectString))
using (var command = new MySqlCommand(sql, connect))
{
connect.Open();
return command.ExecuteScalar().ToString();
}
}
MySql.Data.MySqlClient.MySqlException: 'Unable to connect to any of the specified MySQL hosts.'
MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The error starts at connect.Open();
I've tested the connection with workbench to make sure it's working.
I've looked through most similar post but none have worked for me.
There could many possible scenario's where connection will fail.
Please see below points to troubleshoot your error.
Check your Windows Firewall to ensure that server has an access to Port's associated with your MySql.
Disconnect and re-connect your VPN client.
Follow standard Spacing and Order of parameters in connection string. Follow below format to build/form connection string.
Server=ServerAddress; Port=4563; Database=DataBaseName; Uid=UserName;
Pwd=Password;
Consider using Connection Pulling, So existing connection will be reused.

The right connection string for Remote SQL server for C#

I just want to know the right sql connection string for a remote sql server express edition.
This is what I got but I got some problems
SqlConnection cs = new SqlConnection(#"Data Source=(IP Address)\PC-NAME\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password");
I got this error in my C# debugger:
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: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
Thanks in advance!
From your comment:
The IP address is the static IP address. I am trying to connect outside the building.
If the database you are trying to connect to is on a computer behind a [router/firewall/modem] that has the target address, you will need to use port forwarding on the [router/firewall/modem] to forward all connections on TCP port 1433 through to the target machine.
If you are trying to connect to a home computer behind an ADSL modem with a static IP address, your ADSL modem needs to be configured with port forwarding to connect the external port 1433 with the same port on the internal address. This must be done on the modem, and cannot be done just with a connection string.
Let's say you have an ADSL modem listening on 127.2.3.4 (invalid address for demonstration only) and a PC behind that with an IP address of 192.168.0.100. Configure the modem to forward port 1433 to 192.168.0.100:1433 (how will depend on make and model of modem). Then your connection string would be:
Data Source=127.2.3.4\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password
Assuming that SQLEXPRESS is the only database instance on the server, and since port 1433 is the default, you could use the simpler:
Data Source=tcp:127.2.3.4;Initial Catalog=dbase;User ID=sa;Password=password
The Network Library=DBMSSOCN specification is replaced with tcp: and defaults are assumed for instance and port.
Solution : if you are providing remote machine IP address then you don't need to provide hostname
Try This:
SqlConnection cs = new SqlConnection(#"Data Source=(IP Address)\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password");
Why don't you try SqlConnection String Builder here.

ODBC connection to SQL Server 2012 LocalDB instance

We have a "legacy" application, which uses ODBC connections to an underlying database, which can be Access, Oracle or SQL Server. For unit (or, perhaps more properly, "integration") test purposes, I'd like to hook up a SQL Server 2012 LocalDB instance. However, I cannot figure out a correct ODBC connection string to use.
I have tried:
[TestMethod]
public void OdbcConnectionToLocalDb()
{
string connectionString = "DRIVER=SQL Server Native Client 11.0;Trusted_Connection=Yes;SERVER=(localdb)\v11.0;Description=LocalDB;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
using (var command = new OdbcCommand("select * from Person", connection))
{
connection.Open();
// ...
}
}
}
However, when the connection is opened, the following exception is thrown:
System.Data.Odbc.OdbcException: ERROR [08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [67].
ERROR [HYT00] [Microsoft][SQL Server Native Client 11.0]Login timeout expired
ERROR [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
Is it possible to connect to a SQL Server 2012 LocalDB via an ODBC connection/driver? Is it possible to connect to a specific file?
[EDIT]
Garrett points out it is possible, great. I must have the connection string wrong, so my question really should be: what should the connection string be?
You need to specify your connection string like this:
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=(localdb)\v11.0
The main thing I think is that you reference it as a data source rather than server.
Yes, it's possible. Make sure you install the latest driver: SQL Server Native Client "Denali" (for ODBC and OLE DB).
Look here for more info:
http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx

Can't connect to SQL Server 2005 Express from an ASP.NET C# page

I have an MS SQL Server 2005 Express running on a VPS.
I'm using pymssql in Python to connect to my server with the following code:
conn = pymssql.connect(host='host:port', user='me', password='pwd', database='db')
and it works perfectly.
When I try to connect to the server from an ASP.NET C# page with the following code:
SqlConnection myConnection = new SqlConnection("Data Source=host,port;Network Library=DBMSSOCN; Initial Catalog=db;User ID=me;Password=pwd;");
myConnection.Open();
When I run the ASP.NET page I get the following exception at myConnection.Open();:
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: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
I tried restarting the SQL Server but I had no luck.
Can anyone point me out to what I'm missing here?
Thanks!
Let's say your host is localhost. When you are using SQLEXPRESS you need to specify that in your Instance Name.
Loke this: Data Source=localhost\SQLEXPRESS if it is bound to localhost. Otherwise it might just work with: Data Source=.\SQLEXPRESS.
If you have management studio installed you can fire that up and check what connection string it is using!
Still doesn't work..
I'm able to connect to the remote server with SQL Management Studio.
I enter host,port\SQLEXPRESS (of course I my actual IP number as the host and my actual port) in the Server Name field, select SQL Security and enter my username and password and it works perfectly.
When I try to connect from the ASP.NET page, it just doesn't work - I get the error aforementioned. Can it have something to do with the company hosting my asp.net page? (GoDaddy)
here is the code again..(assuming my host is 11.22.33.44 and my db is named bla
string connectString = #"Data Source=11.22.33.44,1433\SQLEXPRESS;Network Library=DBMSSOCN;Initial Catalog=bla;User ID=username;Password=pwd;";
SqlConnection myConnection = new SqlConnection(connectString);
myConnection.Open();
Thanks again

Categories

Resources