2011-09-15 67 views
5

我正在使用下面的PowerShell腳本連接到sql server的一個實例。我很確定我的用戶名和密碼是正確的。使用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."; } 
} 

它總是落到默認聲明。

回答

4

您對$ conn.open的調用缺少(),所以它會返回對該方法的引用而不是調用它。

+0

謝謝..你釘了它..... :) – Gagan