deepti
deepti

Reputation: 729

powershell connection not opening

I am using powershell to query sql Server. This is the code I am using, it's simple code:

@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()

It gives me an error:

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

Can you please help me out?

Upvotes: 0

Views: 932

Answers (1)

Martin
Martin

Reputation: 1963

In powershell your code would be like this:

$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()

Save this as anything-you-want.ps1 and run it from a powershell console/ise.

Upvotes: 2

Related Questions