简体   繁体   English

如何使用证书颁发机构从C#连接到RDS MySQL数据库?

[英]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. 我正在构建需要连接到AWS RDS MySQL数据库的Xamarin.Mac应用程序( C# )。

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. 但是,通过MySqlWorkbench使用相同的主机和凭据,我可以很好地进行连接。 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). 我看到我的RDS实例是在将证书颁发机构设置为rds-ca-2015情况下构建的(没有选择将其设置为此)。

How can I connect to this RDS DB using the C# code above? 如何使用上面的C#代码连接到该RDS DB?

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 . 因此,对于使用Xamarin.Mac来构建OSX应用程序的其他开发人员,您需要将您的项目设置为使用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. 启动新的OSX项目时,这不是默认设置,因此您必须更改它并重新定位软件包。 Then simply add the NuGet package MySql.Data and update your connection code to: 然后只需添加NuGet包MySql.Data并将您的连接代码更新为:

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. 在那之后就像魅力一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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