gpt4 book ai didi

c# - 使用 SqlConnectionStringBuilder 指定端口?

转载 作者:太空狗 更新时间:2023-10-29 17:53:49 25 4
gpt4 key购买 nike

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

连接字符串如下所示:

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

但是我得到一个看起来像这样的错误:

A network related or instance-specific error has occurred while establishing a connection 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:0 - no connection could be made because the target machine actively refused it.

使用 SqlConnectionStringBuilder.ToString() 验证连接字符串后,结果几乎是 MSDN建议。出于某种原因,它只在数据源周围用双引号引起来,没有别的。然而,这可能只是调试器删除了外部引号,因为它存储在 string 数据类型中。我还验证了 SQL Server 可以在不指定端口的情况下访问,确实如此。最后,我验证了服务器是否允许接受远程连接。考虑到此 SQL Server 实例是在本地安装的,使用默认端口,并且在我的开发计算机上,我无法想象为什么会出现这样的错误。

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

如有任何帮助,我们将不胜感激。谢谢!

编辑:

经过长时间深入研究 SqlConnectionStringBuilder 语法后,它似乎通过将 then 传递给用引号括起来的连接字符串来处理无效参数。我想这是因为它破坏了连接字符串。我的问题仍然存在:有没有办法通过 SqlConnectionStringBuilder 传递端口,还是我必须自己构建它?

最佳答案

长话短说

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

{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 连接(像往常一样),但它将保持未连接状态。然后它尝试一个 udp 连接到端口 1434 并从他们的一个动态端口号接收,该端口号将用于数据将流动的第二个 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

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

然而,如果您需要为您的服务器定义一个明确的端口号,只需避免逗号后的空格,您的连接字符串就会看起来很漂亮。

关于c# - 使用 SqlConnectionStringBuilder 指定端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11289696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com