header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Query SQL 2000 from Powershell V1
Last Post 14 Jan 2009 10:25 AM by Shay Levy. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
Basic Member
Basic Member
Posts:126
Avatar

--
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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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 MillerUser is Offline
    Basic Member
    Basic Member
    Posts:160
    Avatar

    --
    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.


    SynJunkieUser is Offline
    Basic Member
    Basic Member
    Posts:126
    Avatar

    --
    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 MillerUser is Offline
    Basic Member
    Basic Member
    Posts:160
    Avatar

    --
    14 Jan 2009 10:11 AM

    There is an error on this line:

    $cmd.ExecuteNonQuery()

    it should be:

    $sqlcommand.ExecuteNonQuery()

     

    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer