powershell连接没有打开

时间:2016-04-28 06:22:01

标签: sql-server-2008 powershell

我使用powershell来查询sql Server。 这是我正在使用的代码,它是简单的代码:

@echo on

set svr=ax-riscvmssql18\SQL18
set dbname=master

set $cn2 = new-object system.data.SqlClient.SQLConnection("Data Source=ax-riscvmssql18\SQL18;Integrated Security=SSPI;Initial Catalog=master");

$cn2.Open()

它给了我一个错误:

E:\utility\batfiles>$cn2.Open()
'$cn2.Open' is not recognized as an internal or external command, 
operable program or batch file.

你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

在powershell中,您的代码将是这样的:

$svr="ax-riscvmssql18\SQL18"
$dbname="master"

$cn2 = New-Object System.Data.SqlClient.SqlConnection
#$cn2.ConnectionString = "Server=$svr;uid=$user; pwd=$pwd; Database=$dbname; Integrated Security=False;"
$cn2.ConnectionString = "Server = $svr; Database = $dbname; Integrated Security = True" 

$cn2.Open()

将此保存为您想要的任何内容.ps1并从PowerShell控制台/ ise运行它。