Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Trouble parsing SQL data

$
0
0

I have, what I think, is a real head-scratcher.

I am accessing a database to get a list of accounts. Each account has an account_id and account_parent_id property (among several other properties). If the account is the child of another account, the account_parent_id has the account ID of the parent and if the account is a parent (or has no children), the account_parent_id is blank. There are only two levels, so if an account has one or more children, it will not have a parent.

I need the output to be the account number (if the account has no children) and the account number of the parent and all children (comma separated) if there are children. Here is the code I have:

$SQLServer = "<database fqdn>" $SQLDBName = "<databaes name>" $uid ="<username>" $pwd = "<password>" $SqlQuery = "SELECT * from <account table>" $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Persist Security Info = True; User ID = $uid; Password = $pwd;" $SqlConnection.Open() $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) $data = $DataSet[0].Tables $SqlConnection.Close() Foreach ($row in $data) { Foreach ($account in $row) { If ($account.parent_account_id -eq $row.account_id) { $accts += $account.account_id } ElseIf ($account.parent_account_id -eq $account.account_id) { $accts = $account.account_id } Return $accts } } 

The problem is that I’m not getting anything at all into $accts. What am I missing here?

Thanks.

submitted by /u/wetling
[link] [comments]

The post Trouble parsing SQL data appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Latest Images

Trending Articles



Latest Images