TL DR
remove the space before the port number in the data source line:
{Data Source=".\TESTSERVER, 1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}
and let it look like
{Data Source=".\TESTSERVER,1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}
Long answer
After a little game, you can omit the extra quotation marks by removing the space between the comma and the port number:
var stringBuilder = new SqlConnectionStringBuilder(); var sqlCommand = "select TOP 100 * from MyTable;"; stringBuilder.IntegratedSecurity = true; stringBuilder.InitialCatalog = "MyCatalog"; stringBuilder.DataSource = @"myServer\InstanceName,1433"; // This will give the connection string: // "Data Source=myServer\\InstanceName,1433;Initial Catalog=MyCatalog;Integrated Security=True" using (var connection = new SqlConnection(stringBuilder.ToString())) using (var command = new SqlCommand(sqlCommand, connection)) using (var adapter = new SqlDataAdapter(command)) { var table = new DataTable(); connection.Open(); adapter.Fill(table); }
Unfortunately, this still leads to the same error message as the one you provided. So I went deep into network communication and found out that if you do not transmit the port number, it first tries to connect to port 1433 (as usual), but it will remain unconnected. Then he tries to connect udp to port 1434 and receives from his dynamic port number, which will be used for the second tcp connection, in which data will be transmitted.
Using the Process Monitor from Sysinternals, you can watch this process:
// The first try by using TCP port 1433 WindowsFormsApplication.vshost.exe 5480 TCP Reconnect MyMachine:53202 -> SqlServerInstance:1433 SUCCESS WindowsFormsApplication.vshost.exe 5480 TCP Reconnect MyMachine:53202 -> SqlServerInstance:1433 SUCCESS // The second try by using UDP port 1434 WindowsFormsApplication.vshost.exe 7664 UDP Send MyMachine:50245 -> SqlServerInstance:1434 SUCCESS WindowsFormsApplication.vshost.exe 7664 UDP Receive MyMachine:50245 -> SqlServerInstance:1434 SUCCESS // Taking informations out of UDP connection to connect to dynamic assigned port WindowsFormsApplication.vshost.exe 7664 TCP Connect MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Send MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Send MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Send MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Send MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Send MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS WindowsFormsApplication.vshost.exe 7664 TCP Receive MyMachine:53209 -> SqlServerInstance:58904 SUCCESS // Closing of dynamic assigned port WindowsFormsApplication.vshost.exe 7664 TCP Disconnect MyMachine:53209 -> SqlServerInstance:58904 SUCCESS
Using the explicit port number, I will see only the first data, and after that an exception will be thrown. Thus, defining a default port number leads to a different behavior, rather than defining a port number!
However, if you need to determine the explicit port number for your server, just avoid the space after the comma, and your connection string looks pretty.