0

This piece of code executes just fine on Visual Studio 2017 and .NET Core v1.1, but the connection times out on Visual Studio for Mac.

using System;
using System.Data.SqlClient;
using System.Data;

namespace test_console_app
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection connection = new SqlConnection(@"Server=xxxx,1433\xxxx;Database=xxxx;User Id=xxxx;Password=xxxx"))
            {
                using (SqlCommand command = new SqlCommand("Select * from UserAccounts", connection))
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine(((IDataRecord)reader)[1]);
                    }
                }
            }
        }
    }
} 

The exception that is thrown is the following:

"System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)"

Anyone know what the issue might be? Thanks.

Alex Mancheno
  • 31
  • 1
  • 5
  • Try to telnet to the server on that port. It could be a firewall somewhere in between and not a code/.net issue. – MindingData Jul 03 '17 at 19:59
  • File this under probably won't work but just in case?! Have you tried using the IP address instead of hostname? I just had a whole weekends worth of trouble related to my mac not being able to the DNS query properly. – crcrewso Jul 03 '17 at 18:18
  • Yeah, I'v tried using just the ip address. I have something like this: "Server=,1433\", which works on Visual Studio 2017. Were you able to resolve your issue?? – Alex Mancheno Jul 03 '17 at 18:27

1 Answers1

0

can you change this SqlConnection(@"Server=xxxx,1433\xxxx;Database=xxxx;User Id=xxxx;Password=xxxx")) to this SqlConnection(@"Server=xxxx\xxxx,1433;Database=xxxx;User Id=xxxx;Password=xxxx"))

I think you put instance name after port, instead of ip.

Danijel Boksan
  • 779
  • 1
  • 13
  • 30
  • I've tried many different combinations of formatting the connection string, including your suggestion up here and nada. The problem still persists =/ – Alex Mancheno Jul 05 '17 at 20:22
  • Pleaes check this, may will help you: https://stackoverflow.com/questions/44090015/visual-studio-for-mac-connection-to-remote-ms-sql-server-failed – Danijel Boksan Jul 06 '17 at 12:43