使用PowerShell连接到SQL Server

5

我正在使用下面的PowerShell脚本连接到一个SQL Server实例。我非常确定我的用户名和密码是正确的。

$connectionString = "server={0};database={1};uid={2};pwd={3};"

$c = Get-Credential
Write-Host($ipAddress)
Write-Host  $c.username 
Write-Host  $c.GetNetworkCredential().password

$connectionString = [string]::Format( "server={0};database={1};uid={2};pwd={3};", "servername", "databasename",$c.username,$c.GetNetworkCredential().password) 
#open database connection to SQL Server

Write-Host  $connectionString
$conn = New-Object system.Data.SqlClient.SqlConnection
$conn.connectionstring = $connectionString
$conn.open

    switch ($conn.State)
{
"Open" { Write-Host "Do some work"; }
Default { Write-Host "The connection is $($conn.State).  There has been an error connecting to the database."; }
}

它总是落到默认语句。

1个回答

4

你对$conn.open的调用缺少(),所以它将返回该方法的引用而不是调用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接