【问题标题】:can't connect to sql azure database using monotouch无法使用 monotouch 连接到 sql azure 数据库
【发布时间】:2012-12-28 20:54:27
【问题描述】:

我正在使用 c# 和 monotouch 开发一个 iphone 项目。 我需要使用 SQL Azure 数据库。 我的问题是我似乎无法使用单点触控连接。 我可以使下面的代码在使用 Visual Studio 2012 构建在 Windows 8 机器上的本机控制台应用程序中正常工作。 但是,当我尝试将其移植到 imac 并使用 monodevelop/monotouch 时,我的 iphone 应用程序崩溃了。 我得到的错误是:

System.NotImplementedException:客户端和服务器之间发送的数据的 SSL 加密未实现。

我搜索了一下,发现了一个错误报告,似乎描述了我的确切问题here。我注意到它已经快两年了,所以我不确定这是否仍然没有实现。

所以,我尝试更改 StringBuilder.Encrypt = true; 的值为假。 但是,它仍然崩溃,我收到错误:

Mono.Data.Tds.Protocol.TdsInternalException:服务器关闭了连接。 ---> System.IO.IOException: 连接丢失

在任何一种情况下,应用都会在 conn.Open(); 时崩溃;被调用。

我被困住了,我别无选择,只能使用 SQL Azure。 因此,如果有人可以为我的问题提出解决方案或解决方法,我将不胜感激。 提前致谢!

        string userName = "<username>@<myservername>";
        string password = "<password>";
        string dataSource = "<myservername>.database.windows.net";
        string databaseName = "<dbname>";

        SqlConnectionStringBuilder connStringBuilder;
        connStringBuilder = new SqlConnectionStringBuilder();
        connStringBuilder.DataSource = dataSource;
        connStringBuilder.InitialCatalog = databaseName;
        connStringBuilder.Encrypt = true;
        connStringBuilder.TrustServerCertificate = false;
        connStringBuilder.UserID = userName;
        connStringBuilder.Password = password;


        using (SqlConnection conn = new SqlConnection (connStringBuilder.ToString())) {

            conn.Open();

            using (IDbCommand dbcmd = conn.CreateCommand()){
                string sql = "Select client_username from dbo.client;";
                dbcmd.CommandText = sql;
                using (IDataReader reader = dbcmd.ExecuteReader()){
                    while( reader.Read() ){
                        string username = (string) reader["client_username"];
                    }
                }
            }
        }

【问题讨论】:

  • 一般来说,将您的数据库服务器直接暴露在互联网上是一个非常糟糕的主意。首选方法是运行某种(Web)服务层,允许对您的数据进行非常受控的访问。此外,该错误仍然存​​在。
  • 您的问题解决了吗?您是否更改了 Azure 数据库防火墙设置以使您的 iPhone 在允许的范围内?

标签: sql azure mono xamarin.ios


【解决方案1】:

SqlConnection 类的 SSL 未实现。见SqlConnection.cs

由于 Azure 需要加密连接,您必须采取一些解决方法。例如,您可以创建一个 Web 角色并公开一个代表您执行 SQL 的 Web 服务。

【讨论】:

    【解决方案2】:

    尝试使用简单的字符串作为连接字符串:

            connStr = "Server=tcp:<server_here>.database.windows.net,1433;Database=<db_name_here>;Trusted_Connection=False;Encrypt=True;TrustServerCertificate=False;UserID=<user_name_here>@<server_name_here>;Password=<password_here>";
    
            using (SqlConnection conn = new SqlConnection (connStr)) {
    
                conn.Open();
    
                using (IDbCommand dbcmd = conn.CreateCommand()){
                    string sql = "Select client_username from dbo.client;";
                    dbcmd.CommandText = sql;
                    using (IDataReader reader = dbcmd.ExecuteReader()){
                        while( reader.Read() ){
                            string username = (string) reader["client_username"];
                        }
                    }
                }
            }
    

    注意差异 - 使用服务器、数据库。一旦你得到正确的连接字符串,你可以尝试使用 ConnectionStringBuilder 看看你是否得到完全相同的输出。但是,如果你有一个可以工作的字符串,那么只使用一个字符串会更容易。

    【讨论】:

    • 我删除了 SqlConnectionStringBuilder 代码并尝试使用纯字符串。我玩弄弦乐没有成功。但是,我仍然收到错误 - System.NotImplementedException:未实现客户端和服务器之间发送的数据的 SSL 加密。所以,我觉得我正在尝试做单声道无法做到的事情。会是这样吗?有办法解决吗?
    • 这是一个很好的建议,可以减少重复 - 但在单声道中它仍然对我不起作用
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2020-09-29
    • 2020-09-24
    • 2022-01-18
    相关资源
    最近更新 更多