TL;博士
删除数据源字符串中端口号之前的空格:
{Data Source=".\TESTSERVER, 1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}
让它看起来像这样
{Data Source=".\TESTSERVER,1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}
长答案
在玩了一会儿之后,您可以通过删除逗号和端口号之间的空格来省略额外的引号:
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);
}
不幸的是,这仍然会导致与您提供的错误消息相同的错误消息。因此,我深入研究了网络通信并发现,如果您不传递端口号,它首先会尝试与端口 1433 建立 tcp 连接(像往常一样),但它将保持未连接状态。然后,它尝试与端口 1434 建立 udp 连接,并从它们那里接收一个动态端口号,该端口号将用于数据将流向的第二个 tcp 连接。
通过使用 Sysinternals 的 Process Monitor,您可以查看此过程:
// 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
通过使用显式端口号,我只会看到给定的第一行,然后抛出异常。因此,定义默认端口号会导致另一种行为,而不是不定义任何端口号!
尽管如此,如果您需要为您的服务器定义一个明确的端口号,只需避免逗号后面的空格,您的连接字符串就会看起来很漂亮。