Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
Using PowerShell
>
SQL Server
Query SQL 2000 from Powershell V1
Last Post 14 Jan 2009 10:25 AM by
Shay Levy
. 5 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
SynJunkie
Basic Member
Posts:126
14 Jan 2009 05:16 AM
Hi
I am trying to automate my leaver process and one of the last things I need to do is remove a record from a SQL table. I have found the following script on the web and I am able to use it to query a SQL database successfully (as shown in example 1) but it will not delete a record using the syntax as shown in example 2.
Example 1
$Username = "ALFKI"
$SqlServer = "sqlserver"
$SqlCatalog = "Northwind"
$SqlQuery = "Select * from dbo.customers where customerID='$Username'"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlCatalog; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
Clear
$DataSet.Tables[0]
Example 2
$Username = "ALFKI"
$SqlServer = "sqlserver"
$SqlCatalog = "Northwind"
$SqlQuery = "Delete from dbo.customers where customerID='$Username'"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlCatalog; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
Clear
$DataSet.Tables[0]
The error I recieve is:
Unable to index into an object of type System.Data.DataTableCollection.
At C:\scripts\SQL-TEST.ps1:17 char:17
+ $DataSet.Tables[0 <<<< ]
Does anyone know why one query would work but not another?
Thanks
Lee
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
14 Jan 2009 06:53 AM
I don;t have the time right now to delve into it but try this, it is much simpler:
$Username = "ALFKI"
$SqlServer = "sqlserver"
$SqlCatalog = "Northwind"
$SqlQuery = "Delete from dbo.customers where customerID='$Username'"
$connString = "Data Source=$sqlServer; Initial Catalog=$SqlCatalog; Integrated Security=SSPI"
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
$sqlCommand = New-Object System.Data.SqlClient.sqlCommand $SqlQuery,$conn
$conn.open()
$cmd.ExecuteNonQuery()
$sqlCommand.ExecuteNonQuery()
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
Chad Miller
Basic Member
Posts:160
14 Jan 2009 08:00 AM
As to the reason why, the delete query does not return a DataTable (no result set). You would see a similar error message if you referenced an non-existent array element i.e. $z[0]. I've tested your delete statement and it does complete using your original code, the $DataSet.Tables[0] line causes an error, however the delete completes.
As Shay pointed out, the preferred method is to use ExecuteNonQuery for queries which do not return a result set.
SynJunkie
Basic Member
Posts:126
14 Jan 2009 09:36 AM
Thanks for the suggestions but I still recieve an error.
You cannot call a method on a null-valued expression.
At C:\scripts\sqlv2.ps1:11 char:21
+ $cmd.ExecuteNonQuery( <<<< )
Any other ideas?
Chad Miller
Basic Member
Posts:160
14 Jan 2009 10:11 AM
There is an error on this line:
$cmd.ExecuteNonQuery()
it should be:
$sqlcommand.ExecuteNonQuery()
Shay Levy
PowerShell MVP, Admin
Veteran Member
Posts:1362
14 Jan 2009 10:25 AM
Thanks Chad, bad paste :-)
Shay Levy
Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter:
@ShayLevy
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
Using PowerShell
>
SQL Server
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2