使用SqlConnectionStringBuilder指定端口?

时间:2012-07-02 07:54:31

标签: c# sql-server sql-server-2008-r2 database-connection connection-string

我遇到了障碍。我需要为本地安装的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传递端口,还是我必须自己构建它?

4 个答案:

答案 0 :(得分:5)

TL; DR

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

{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);
}

不幸的是,这仍然会导致与您提供的错误消息相同的错误消息。所以我深入研究了网络通信并发现,如果你没有传入一个端口号,它首先尝试一个tcp连接到端口1433(像往常一样),但它将保持不连接状态。然后它尝试连接到端口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

通过使用explict端口号,我只会看到第一个给定的行,然后抛出异常。因此,定义默认端口号会导致另一种行为,而不是定义任何端口号!

然而,如果您需要为服务器定义显式端口号,只需避开逗号后的空格,连接字符串看起来很漂亮。

答案 1 :(得分:0)

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

即可

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

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

答案 2 :(得分:0)

MSDN SqlConnection.ConnectionString Property

要连接的SQL Server实例的名称或网络地址。可以在服务器名称后指定端口号:

示例:MyDataSource,1433

答案 3 :(得分:0)

对我来说,它的工作是这样的:

//read instance
builder.DataSource.Split(new Char[] { ',' })[0]
//read port number
builder.DataSource.Split(new Char[] { ',' })[1]

//write
builder.DataSource = builder.DataSource.Split(new Char[] { ',' })[0] + ",1234";