none
System.data.dataset Problem RRS feed

  • Question

  • Hi All : I am writing the following :

     

    Add-Type -Path 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Smo\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll'

    $finalDataSet = New-Object System.Data.Dataset

    get-content 'C:\Serverlist.txt' | where { ($_ -eq "server2") -or ($_ -eq "server1")  } | foreach-object {

    $SQLServer=$_
    $SQLDBName = "master"
    $SqlQuery = "select * from master.sys.databases"

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; 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] | select name
    $finaldataset.Tables.add($dataset.tables[0])

    }

    $finaldataset | select name

    but get the following error:

    Exception calling "Add" with "1" argument(s): "DataTable already belongs to another DataSet."
    At line:24 char:25
    + $finaldataset.Tables.add <<<< ($dataset.tables[0])
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Any problem in the script??

    Tuesday, April 8, 2014 7:43 AM