使用SqlConnectionStringBuilder指定端口?

Dav*_*d W 9 c# sql-server connection-string database-connection sql-server-2008-r2

我遇到了麻烦.我需要为本地安装的SQL Server 2008 R2指定端口号.到目前为止,我已经尝试使用SqlConnectionStringBuilder并将数据源设置为.\TESTSERVER, 1433,所有文档都应该将我连接到端口1433上的服务器TESTSERVER(默认值).

连接字符串如下所示:

{Data Source=".\TESTSERVER, 1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}

但是我收到的错误如下:

建立连接SQL Server时发生网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确以及SQL Server是否配置为允许远程连接.(提供者:TCP提供者,错误:0 - 无法建立连接,因为目标计算机主动拒绝它.

在使用SqlConnectionStringBuilder.ToString()它验证连接字符串时,几乎可以看出MSDN的建议.由于某种原因,它仅包含数据源周围的双引号而不包含任何其他内容.但是,这可能只是调试器删除外部引号,因为它存储在string数据类型中.我还验证了SQL服务器是可以访问的,而无需指定端口,它是.最后,我验证了服务器可以接受远程连接.考虑到这个SQL Server实例是在本地安装的,使用默认端口,在我的开发计算机上,我无法想象为什么我会收到这样的错误.

这是因为SqlConnectionStringBuilder是用它自己的端口覆盖我的端口吗?我是否需要出于某种原因在我的防火墙上打开端口?知道它是完全本地安装,它不应该遇到防火墙问题.我宁愿不必手动构建连接字符串.并不是说它很难,它只是给我的代码增加了一层复杂性,除非需要,否则我不愿意.

任何帮助,将不胜感激.谢谢!

编辑:

经过SqlConnectionStringBuilder语法的长时间艰难挖掘后,它似乎通过传递给引号中包含的连接字符串来处理无效参数.我想这是因为它破坏了连接串.我的问题仍然存在:有没有办法通过SqlConnectionStringBuilder传递端口,还是我必须自己构建它?

Oli*_*ver 6

TL; DR

删除数据源字符串中端口号之前的空格:

{Data Source=".\TESTSERVER, 1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}
Run Code Online (Sandbox Code Playgroud)

并让它看起来像这样

{Data Source=".\TESTSERVER,1433";Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE}
Run Code Online (Sandbox Code Playgroud)

长答案

玩了一会儿之后,您可以通过删除逗号和端口号之间的空格来省略多余的引号:

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);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这仍然导致与您提供的错误消息相同的错误消息。因此,我对网络通信进行了更深入的研究,发现,如果您不传入端口号,它将首先尝试与端口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
Run Code Online (Sandbox Code Playgroud)

通过使用显式端口号,我将仅看到前几行,然后抛出异常。因此,定义默认端口号会导致另一种行为,而不是未定义任何端口号!

但是,如果您需要为服务器定义一个明确的端口号,则只需避免在逗号后留空格,并且连接字符串看起来很漂亮。


Fra*_*iee 0

只需看一下http://www.connectionstrings.com/sql-server-2008

SQL Server 2008 连接字符串在服务器实例名称周围不包含额外的引号。你的连接字符串应该是

Data Source=.\TESTSERVER, 1433;Initial Catalog=AdventureWorks;User ID=testuser;Password=MYPASSWORDHERE
Run Code Online (Sandbox Code Playgroud)

  • 我再次使用 SqlServerConnectionStringBuilder。我不是亲自构建这个。我想知道为什么当我使用 Microsoft 自己的解决方案来构建可靠的连接字符串时会发生这种情况。 (3认同)