使用 powershell 连接到 SQL Server

发布于 2024-12-05 00:31:36 字数 840 浏览 1 评论 0原文

我正在使用下面的 powershell 脚本连接到 sql server 的实例。我非常确定我的用户名和密码是正确的。

$connectionString = "服务器={0};数据库={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."; }
}

它总是落入默认语句。

I am using this powershell script below to connect to an instance of sql server. I am pretty sure that my username and password are correct.

$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."; }
}

It is always falling to the default statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2024-12-12 00:31:36

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

Your call to $conn.open is missing the (), so it would be returning a reference to that method rather than calling it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文