Pizhev Racing
Pizhev Racing

Reputation: 486

How to connect to Cloud SQL using Xamarin (iOS project) on Visual Studio

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 ?

Upvotes: 0

Views: 247

Answers (1)

nevermore
nevermore

Reputation: 15786

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.

Upvotes: 2

Related Questions