1

OK ... I am tearing my hair out here (and i dont have much to start with)..and its one of those Developer Seal of Approval where "It works on my machine"

Basically, I have a C# Console app that needs to upload data to a SQL Database hosted in Azure. My Dev database is held in a Visual Studio subscription (the free one with MSDN) and the Prod database is in our corporate Azure subscription (which I have to rely on our Azure admin team to configure).

On my PC, under Visual Studio (or CMD line) I can connect to BOTH instances, however, on my PROD server, I CAN connect to the DEV database, but NOT the PROD DB... using exactly the same test app (see below).

Now, i AM behind a company firewall / proxy but the IP addresses have been added to the both database's firewall rules ... and its obviously configured properly because it can connect from by Dev PC.

To try and make testing simple, I pared everything back down to a simple test C# test app and ran it with the appropriate connect strings...

using System;
using System.Data.SqlClient;

namespace AzureConnectTest
{
    class AzureConnectTest
    {
        static void Main(string[] args)
        {
            string ConnectionString = @"data source=******.database.windows.net; ";
            ConnectionString += @"initial catalog=****; ";
            ConnectionString += @"persist security info=False; ";
            ConnectionString += @"MultipleActiveResultSets=True; ";
            ConnectionString += @"user id=""*****""; ";
            ConnectionString += @"password=""*****""; ";

            Console.WriteLine("Attempting to connect");
            Console.WriteLine(ConnectionString);
            try
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    Console.WriteLine(" - Success");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(" - Failed");
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Now ... here's the outcome...

enter image description here

So ... what have I missed here ....

For the record, I have searched SO for similar questions but they either have not been answered or the responses provided still have not solved the issue ... and you can't "bump" questions to say "I'm having this problem"

3
  • Hi. If you don't have access to Azure than it is quiet a challenge to find out what is configured. It could be for example that you need Encrypt=True; and probably TrustServerCertificate=False. To find it out quicker and how to connect I would suggest you to use sql management studio and try to connect to the azure database there. Commented May 22, 2018 at 12:52
  • @Mono Tried the two parameters (all combinations) but still does not connect from prod server - Our Azure team are not developers and keep saying "have you tried from SSMS" but its not installed on the prod server and I am reluctant to install it "just to test the connection" Commented May 22, 2018 at 13:03
  • Error in second row says that there is invalid keyword authentication in connection string. And third row error says that the sql server is not connectable. Commented May 22, 2018 at 13:14

1 Answer 1

2

To connect successfully to an Azure SQL Database, your connection string needs the following components:

Server=tcp:***server***.database.windows.net,1433;
Initial Catalog=***database***;
Persist Security Info=False;
User ID=***user id***;
Password=***password**
MultipleActiveResultSets=False;
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=30;

Please note that in order for a connection to be brokered the IP address of the connecting machine must be granted a firewall exception.

Sign up to request clarification or add additional context in comments.

3 Comments

Well.... good news, bad news ... The good news is that changing to this connect sting construct solved the issue on my production server ... However, it does not work on my dev PC ... But at least I can just set it in App.Config for each environment
@ChrisHammond happy to hear it! I suspect it's a firewall issue. I updated my answer accordingly
aaaannndddd... its stopped again ... so I am now starting to think the same @mathijspim ... but the the IP addresses being presented to Azure from the server (via Firewall/Proxy/NAT) are added into the DB exceptions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.