How to connect Google Cloud PostgreSQL with C# - c#

I created a managed sql instance on Google Cloud using these instructions.
I would like to connect from my code using C#.
When I write locally, this is my connection string:
public string LocalDataSource = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\Bob\Desktop\versionsOfMediaPlayer\14.4.19\mediaPlayer_1\Database1.mdf';Integrated Security=True;Connect Timeout=30";
and I manage to write to local sql.
What should be my connection string for the instance on Google Cloud?
details of my instance:
ip: <MY_IP_ADDRESS>
user: postgres
password: postgres
Instance connection name: my-project:us-central1:my-sql-instance
I added my local ip to the Cloud SQL instance firewall - so I manage to connect via command line, now I'd like to do so programmatically, using C#.
what should be my connection string?

Once you have authorized your IP address to connect, connecting to Cloud SQL is the same as any other database. According the the MySQL Connector docs for C# (found here), the connection string should look like the following:
"server=<YOUR_IP_ADDRESS>;uid=postgres;pwd=postgres;database=your-database";

Related

How do I connect to my Google cloud SQL instance with C#?

I am trying to use visual studio to write a program in c# and have it connect to my google cloud sql server. Unfortunately I have been trying to troubleshoot it and have had no luck. I already added my IP and my connection works through MYSQL Workbench.
I attempted to connect with the whole data source, network library, initial catalog, etc. I tried it without the network library and used the proper way to escape \ in the string during the connection. I also tried the default 1433 port and 3306. No luck.
// Build Connection String
SqlConnection clearviscon = new SqlConnection(#"Server=xx.xx.xx.xx\alpine-park-243102:us-central1:xxx, 3306;Network Library=DBMSSOCN;Initial Catalog=xxdatabase;User ID=root;Password=xxx123");
clearviscon.Open();
To connect to Google Cloud SQL for MySQL, you must use a MySQL client library. I recommend MySqlConnector.
Your connection string will be Server=xx.xx.xx.xx;Database=xxdatabase;User ID=root;Password=xxx123.
If you're using a client SSL certificate to connect to your server, add this to the end of your connection string: ;SslCa=server-ca.pem;SslCert=client-cert.pem;SslKey=client-key.pem.
You must use MySqlConnection.
See this link for more information: https://cloud.google.com/dotnet/docs/getting-started/using-cloud-sql

Connection String via IP Address For SQL Server

I want to create connection string so that I can connect to SQL database remotely in my C# code using OleDb as a provider and the server IP address.
Note 1 : I'm using OleDb because I want to handle different database types. SQL is just an example.
Note 2 : The database resides on another machine (machine on another network)
I did all the setup to connect from another machine (firewall,Enable TCP/IP..etc), and now I can connect remotely using Microsoft SQL Server Management 2014 by specifying (server name : My Computer Name-PC\Instance name) and using SQL Authentication then entering the username and password and press connect and then it goes well, I connect successfully.
BUT I TRIED A LOT OF COMBINATIONS TO BUILD THE CONNECTION STRING IN MY C# CODE AND NONE OF THEM WORKS EXCEPT THIS :
OleDbConnection conn = new OleDbConnection(#"provider = sqloledb; data source = MyCompName-PC\sqlexpress; Initial Catalog = DataBase1 ; user id = MyUsername ; password = MyPassword ;");
Otherwise if I try to use my server public IP address instead of MyCompName in Data Source it keeps giving me error : server not found or access denied.
I search in connectionstrings.com but problem is still there.
From your post it looks like you are trying to connect to a SQL Server database then why are you using OleDbConnection? instead of using SQL Server connection provider.
OleDbConnection connection provider is used for connecting to MS Access database.
You should be using a SqlConnection class like
SqlConnection conn = new SqlConnection("data source = MyCompName-PC\sqlexpress; Initial Catalog = DataBase1 ; user id = MyUsername ; password = MyPassword ;")
See SQLConnection Reference for more information as commented by #AlexK.

How connect C# to Postgresql in host j.layershift.co.uk

I have installed postgresql database in http://postgres-project-1241043.j.layershift.co.uk/ host.
I want to connect to the database using C#. I use Npgsql with following connection string.
connectionString = # "Server = postgres-project-1241043.j.layershift.co.uk, Port = 5432, User Id = postgre; Password = abcdef; Database = dbluanvantn;";
But I am not able to connect to the server and get error:
Npgsql.NpgsqlException: Failed to a connection to
'postgres-project1241043.j.layershift.co.uk'.
Am I using correct connection string?. Help me fix it.
You can only connect to Postgres (on our Jelastic service) if you add a public IP to the node first. Without that step, you can only connect to it locally (i.e. from another server within your Jelastic environment).
Also I want to mention that you are always welcome to contact our support team (our tech. support is 24x7 and completely free; even for our trial accounts) if you need any further help.

How to connect the oracle database with .net?

How to connect the oracle database as a backend in asp.net (C#)?
what is the connection string for it?
when i was trying to connect i got the below error:
ORA-12154: TNS:could not resolve the connect identifier specified
The actual connection string depends on the parameters of your server (ip, instance name, credentials, etc).
Here's a site with several 'example' connection strings for oracle:
http://www.connectionstrings.com/oracle
To connect to Oracle DB with .NET, best way is:
Install Oracle client
Configure tnsNames.ora
Reference .net project to Oracle.DataAccess.Dll (ODP.NET)
Create connection using OracleConnection object
I will not even go into other possibilities because this is golden standard for .net-Ora connectivity

connect mysql with c#

I'm building a c# windows application which will connect with the mysql database in a remote server.
I'm using the following connect script
string connectionString;
connectionString = "SERVER = eu5.org;UID = myuserid; PASSWORD = mypassword; DATABASE = mydatabasename;";
connection = new MySqlConnection(connectionString);
It shows the error couldn't connect to the database.
P.S: Mysql database is at eu5.org server
Personally I prefer to use the MySql Workbench IDE for testing connections and working (Querying) the database directly where possible. Most hosted databases that I have worked with normally define the Server as Instance.[DomainName] so I would have expected your server URL to be something like MySql1.eu5.org
Below is a connection string that I tested using the MySql Connector, change the parameters.
<connectionStrings>
<add name="MySqlConnection" connectionString="server=INSTANCENAME.DOMAINNAME.COM;UID=USERNAME;password=PASSWORD;database=DATABASENAME;Persist Security Info=True;" providerName="MySql.Data.MySqlClient"/>
MAke sure that your server should be started...
Just read the FAQ:
Many people want to only use database, but their site is hosted elsewhere. We provide free database for websites hosted with us. For that reason, external access is blocked without exception.
--> http://www.freewebhostingarea.com/faq.html
so it's not possible to access your MySQL-DB from external sources.
only localhost will be allowed
Your code is correct.
Use a browser for MySQL, like http://mysql-query-browser-for-windows.apponic.com/ to check if your database is available.

Categories