简体   繁体   中英

Connection to SQL Server through Powershell (not local)

I'm trying to connect to a remote SQL Server instance using PowerShell.

My username is ONE_PERSON .

This is the code I am using:

$user = "ONE_PERSON" 
$connectionString = "Server=$dataSource; uid=$user; pwd=$pwd; Database=$database; Integrated Security = False;" 
$connection = New-Object System.Data.SqlClient.SqlConnection 
$connection.ConnectionString = $connectionString 
$connection.Open()

When I run this, I get an error

Login failed for user 'ONE_Person'

For some reason it switches my user to lowercase even though when I run $user on its own, it still shows me in uppercase ONE_PERSON .

I even tried forcing it to stay uppercase by using .ToUpper at the end

And insights would be appreciated

$pwd is a reserved path variable in PowerShell. Try saving your variable as something like $password instead and seeing if that works.

1.Create a connection

2.Create your command (the T-SQL that will be executed)

3.Create your data adapter (if you want to retrieve data)

4.Create your dataset (the adapter fills this object)

Create a Connection You simply create an object of System.Data.SqlClient.SqlConnection and pass the connection string that will be used to connect to the given SQL Server instance…don't forget to open it.

$sqlConn = New-Object System.Data.SqlClient.SqlConnection
$sqlConn.ConnectionString = “Server=localhost\sql12;Integrated Security=true;Initial Catalog=master”
$sqlConn.Open()

Create Your Command You have a few options here because the SqlConnection actually contains a method that you can use to create your command object, or you can create a separate object all together. I have seen both options used so it is for the most part a preference.

$sqlcmd = $sqlConn.CreateCommand()
<# or #>
$sqlcmd = New-Object System.Data.SqlClient.SqlCommand
$sqlcmd.Connection = $sqlConn
$query = “SELECT name, database_id FROM sys.databases”
$sqlcmd.CommandText = $query

Create Your Data Adapter By definition, this object “represents a set of data commands and a database connection that are used to fill the DataSet”. You create the SqlDataAdapter and pass it to the previous command object created, $sqlcmd.

$adp = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcmd

Create Your DataSet (and fill it) This object will be the type of System.Data.DataSet and as defined is simply “an in-memory cache of data”. Which this is something to take note of that the query you are running has to be loaded into memory, so the larger the dataset the more memory needed.

$data = New-Object System.Data.DataSet
$adp.Fill($data) | Out-Null

Retrieving Your Data After you do all that you are probably wondering how you output that data so you can see it? The data itself resides in a collection of tables within the Table property of your DataSet object. Now depending on the version of .NET you are working with you might actually need to specify the index of the collection (eg Tables[0]), but this is generally only required in older versions below .NET 4.0.

$data.Tables
<# or #>
$data.Tables[0]

For more information watch this video https://youtu.be/P01_Zd11HX0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM