0

I read the documentation on Cloud SQL here but in the solution explorer I not have appsettings.json file to put this code:

  {
  "CloudSQL" : {
     ConnectionString": "Host=127.0.0.1;Uid=DATABASE_USER;Pwd=PASSWORD;Database=DATABASE_NAME"
  }
}

Then, in your Startup.cs file, create a database connection:

var connectionString = new MySqlConnectionStringBuilder(
    Configuration["CloudSql:ConnectionString"])
{
    // Connecting to a local proxy that does not support ssl.
    SslMode = MySqlSslMode.None,
};
DbConnection connection =
    new MySqlConnection(connectionString.ConnectionString);

I use empty blank project for iOS on Visual Studio on Mac. How to connect to Cloud SQL ?

Pizhev Racing
  • 466
  • 1
  • 6
  • 23
  • You do not need to get the connection string from the configuration file. You could hard code the connection string into the c# application but then if connection string changes you have to recompile. Putting into the configuration file makes it easy to change without recompiling. – jdweng Nov 13 '20 at 11:53
  • connecting directly to a db server from a client is generally a horrible idea and you should not do it – Jason Nov 13 '20 at 12:06
  • Does this answer your question? [How to connect Google Cloud SQL with C#](https://stackoverflow.com/questions/52457709/how-to-connect-google-cloud-sql-with-c-sharp) – Samuel Romero Nov 13 '20 at 23:51
  • Does my solution work for you? If yes, can you please accept it (click the ☑️ in the upper left corner of this answer ) so that we can help more people with same problem:). – nevermore Nov 18 '20 at 09:35

1 Answers1

2

You can hard code the connectionString in code behind:

    var connectionString = new MySqlConnectionStringBuilder("Host=127.0.0.1;Uid=DATABASE_USER;Pwd=PASSWORD;Database=DATABASE_NAME")
    {
        // Connecting to a local proxy that does not support ssl.
        SslMode = MySqlSslMode.None,
    };
    DbConnection connection = new MySqlConnection(connectionString.ConnectionString);

Well, it is not recommend to connect directly to a db server from a client. It's unsafe.

nevermore
  • 15,432
  • 1
  • 12
  • 30