`function read_DB {
Param(
[Parameter(Mandatory = $true, Position = 0)][string]$query,
[Parameter(Mandatory = $true, Position = 1)][System.Data.SqlClient.SqlConnection]$connection,
)
$table = New-Object System.Data.DataTable
$connection.Open()
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$dataAdapter.Fill($table)
$connection.Close()
return $table
}
**Main**
$all_tasks = New-Object 'System.Collections.ArrayList'
$query = "SELECT * from dbo.SPO_All where Status='Pending'"
$all_tasks = read_DB -query $query -connection $database_connection`
`Output :-
PS C:UsersAdministratorDocuments> $all_tasks
3
ID : 4
Date : 9/23/2024 10:28:26 PM
siteUrl : yaxxxx.com
insertedBy : xxxx
Status : Pending
ID : 5
Date : 9/23/2024 10:28:47 PM
siteUrl : axxx.com
insertedBy : xxxx
Status : Pending
ID : 6
Date : 9/23/2024 10:29:08 PM
siteUrl : goxxxx.com
insertedBy : pxxx
Status : Pending
`
I am fetching data from the SQL Table through read function and trying to save it in array.However the first item it showing in the list is the count of table row.I am completely clueless why it is doing that?`
2