简体   繁体   中英

How can I connect to an RDS MySQL DB from C# using a Certificate Authority?

I am building a Xamarin.Mac application ( C# ) which needs to connect to an AWS RDS MySQL database.

I have the code:

using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM store", connection))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("{0} {1} {2}",
                            reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    }
                }
            }

Where my connection string is of the format:

ConnectionString = "Server={0};Database={1}Uid={2};Pwd={3}";

However, I can't seem to connect. Yet, using the same host and credentials via MySqlWorkbench, I can connect just fine. I saw that my RDS instance was build with the setting Certificate Authority set to rds-ca-2015 (there is no option to not set it as this).

How can I connect to this RDS DB using the C# code above?

Turns out this does not have to do with certificate authority as I initially thought.

However, I figured I'd post an answer because it took me a long time to find a solution, and other devs will hopefully find it useful.

So for other developers who are using Xamarin.Mac to build an OSX application, you need to set your project to use Xamarin.Mac .NET 4.5 Framework . This is not the default when starting a new OSX project, so you must change it and retarget your packages. Then simply add the NuGet package MySql.Data and update your connection code to:

using (MySqlConnection connection = new MySqlConnection(ConnectionString))
        {
            connection.Open();
            using (MySqlCommand command = new MySqlCommand("SELECT * FROM store", connection))
            using (MySqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1} {2}",
                        reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                }
            }
        }

Works like a charm after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM