Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Can';t在端口1433上连接到SQL Server数据库服务器_Sql Server_Powershell_Tcp_Enterprise_Ssms 2017 - Fatal编程技术网

Sql server Can';t在端口1433上连接到SQL Server数据库服务器

Sql server Can';t在端口1433上连接到SQL Server数据库服务器,sql-server,powershell,tcp,enterprise,ssms-2017,Sql Server,Powershell,Tcp,Enterprise,Ssms 2017,我正在尝试为任务编写PowerShell SQL查询脚本,但在此之前,我正在测试以确保我的DB连接正常工作 我已经使用SSMS 17在SQL Server中创建了一个表,作为连接测试的一部分,我正在测试是否可以在端口1433(也在防火墙规则中打开)上连接到数据库服务器 这是我用来测试与SQL Server的端口连接的代码段: $port = 1433 $tcp = New-Object Net.Sockets.TcpClient if ([void]$tcp.Connect($dbhost

我正在尝试为任务编写PowerShell SQL查询脚本,但在此之前,我正在测试以确保我的DB连接正常工作

我已经使用SSMS 17在SQL Server中创建了一个表,作为连接测试的一部分,我正在测试是否可以在端口1433(也在防火墙规则中打开)上连接到数据库服务器

这是我用来测试与SQL Server的端口连接的代码段:

$port   = 1433

$tcp = New-Object Net.Sockets.TcpClient
if ([void]$tcp.Connect($dbhost, $port)) {
  'connected'
} else {
  'not connected'
}
$tcp.Dispose()
其中
$dbhost
=myservername.domain.com

每次运行脚本时,它都会返回:

不相连

为什么呢

我检查了SSMS中的服务器产品及其使用

Microsoft SQL Server Enterprise: Core-based Licensing (64-bit)

我之所以提到这一点,是因为一些在线解决方案提到了服务器和实例,如果我有SQL server Express,我必须将Express列为主机名或其他内容的一部分。但是我用的是企业版,所以我猜它是默认的
MSSQLServer
,它不必被指定为dbhostname的一部分您可能没有及时连接,但正在尝试检查状态是否当前已连接。尝试对
Net.Sockets.TcpClient
类使用
BeginConnect
方法,它有超时选项,可能会对您有所帮助。我已修复了您的代码:

$port   = 1433
$timeout = 1000 #ms

$tcp = New-Object Net.Sockets.TcpClient
$wait = $tcp.BeginConnect($dbhost,$port,$null,$null)
[void]$wait.AsyncWaitHandle.WaitOne($timeout,$false)
if ($tcp.Connected) {
  'connected'
} else {
  'not connected'
}
$tcp.Close()
$tcp.Dispose()
返回
void
,因此PowerShell
if
语句的计算结果永远不会是
$true
。改为在连接后检查
Net.Sockets.TcpClient.Connected
属性:

$port   = 1433

$tcp = New-Object Net.Sockets.TcpClient
$tcp.Connect($dbhost, $port)
if ($tcp.Connected) {
  'connected'
} else {
  'not connected'
}
$tcp.Dispose()
请注意,如果连接尝试失败,将引发异常,因此
if
是多余的。您可以改为使用try/catch:

$port   = 1433

$tcp = New-Object Net.Sockets.TcpClient
try {
    $tcp.Connect($dbhost, $port)
    $tcp.Dispose()
    'connected'
} catch [System.Net.Sockets.SocketException] {
    $_.Exception.ToString()
    'not connected'
}

使用原始TCP进行Sql Server连接没有什么意义。如果它连接起来,你所能知道的就是有东西在接受连接。除非您计划引入Sql Server使用的TDS协议,否则更好的方法可能是使用
Invoke SqlCmd
运行
select@@version
或者类似的东西?@vonPryz嗯,我真的不知道那些像TDS这样的高级概念,我基本上只是说要在powershell中深入研究sql,所以我在遵循另一个线程来做我应该做的事情。这家伙有一个测试端口然后测试连接的功能,所以我只是跟着。我的理解是,如果这个函数在1433年返回连接成功,那么我应该能够解决当前的问题,我与测试sql连接函数,因为它也返回未连接状态,所以我认为这是因为端口1433 Issuedode,你给了我最大的提示!显然,当我传递$dbhost时,它一直是空的!我不认为powershell关心顺序,所以我将dbhost设置为一个文件,并在脚本的后面一直使用值读取,所以当然dbhost没有得到任何值,因为我必须在它之前设置变量!非常感谢。