can't connect to sql azure database using mono

2019-08-08 19:29发布

问题:

I'm working on a iphone project using c# and monotouch. I need to use an SQL Azure database. My problem is that I cant seem to get connected using monotouch. I can make the code below work fine in a native console application built on a windows 8 machine using visual studio 2012. But, when I try to port it over to an imac and use monodevelop/monotouch my iphone app crashes. The error I get is:

System.NotImplementedException: SSL encryption for data sent between client and server is not implemented.

I google around a bit and found a bug report that seems to describe my exact issue here. I noticed its almost two years old so i'm not sure if this would still be unimplemented.

So, I tried changing the value of StringBuilder.Encrypt = true; to false. But, it still crashes and I get the error:

Mono.Data.Tds.Protocol.TdsInternalException: Server closed the connection. ---> System.IO.IOException: Connection lost

in either case the app crashes when conn.Open(); is called.

I'm pretty stuck, and I don't have a choice but to use SQL Azure. So, if anyone could suggest a solution or work around for my issue, I'd appreciate it greatly. thanks in advance!

        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"];
                    }
                }
            }
        }

回答1:

SSL for SqlConnection class is not implemented. See SqlConnection.cs

And as Azure requires an encrypted connection you'll have to do some workaround. For example you could create a web role and expose a web service which executes the SQL on your behalf.



回答2:

try using a simple string as your connection string:

        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"];
                    }
                }
            }
        }

Note differences - use Server, Database. Once you get the connection string correct, you can use try the ConnectionStringBuilder and see if you get the exact same output. BUt its easier to just use a string if you have one that works.