0

我正在尝试通过 Powershell 连接 SQL 服务器。

$Server = 'RDS End Point,PortNO'
$Database = 'DBName'
$uid ='UserID'
$pwd = 'Password'

$connstring = 'Server=$Server; Database=$Database; '
$connstring += 'User ID=$uid; Password=$pwd;'

$connection = New-Object System.Data.SqlClient.SqlConnection($connstring)
$connection.Open()

Exception calling "Open" with "0" argument(s): "A network-related or instance-specific error occurred while
establishing a connection to 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: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server)"
At line:1 char:1
+ $connection.Open()
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException

怎么了?

4

1 回答 1

1

只有双引号字符串在 PowerShell 中进行插值。这个

$connstring = 'Server=$Server; Database=$Database; '
$connstring += 'User ID=$uid; Password=$pwd;'

应该

$connstring = "Server=$Server; Database=$Database;User ID=$uid; Password=$pwd;"
于 2020-05-12T12:19:12.787 回答