0

sqldatareader is not reading rows when placed inside while loop construct . there is one row returned from the from the sql query which I verfied in DB. when I read it simply without any construct , its reading the data. The following code peice when placed above the while loop construct gives me data in the log file. while loop will not get activated as there is only one row and that is ok.

$sqloutqryreader = MsSqlQueryExecutor -SqlQuery $SqlQuery -logfile $logfile
    $sqloutqryreader.Read() 
                    $InputFileLst = $sqloutqryreader["InputFiles"]  
                    Add-Content -Value "$TimeinSec Log: Reading data from sql reader $InputFileLst" -Path $logfile          


                    $rdrRowCnt = 0                          

                    while($sqloutqryreader.Read()) 
                        {   
                            $rdrRowCnt++

                            if($rdrRowCnt -gt 1)
                                {
                                    Add-Content -Value "$TimeinSec Ambiguity: two Records are found for the current job: $jobname in the $mastTableNm table, hence aborting" -Path $logfile
                                    exit
                                }
                            $InputFileLst = $sqloutqryreader["InputFiles"].Split(";")
                            $OutputFileLst = $sqloutqryreader["OutputFiles"].Split(";")
                            Add-Content -Value "$TimeinSec Log: Input files:$InputFileLst and outputfiles:$OutputFileLst found for the current job: $jobname in the $mastTableNm table" -Path $logfile
                        }   

however when i place the following while loop construct, while loop is not activated. Since there is one row, it has to get activated.

$sqloutqryreader = MsSqlQueryExecutor -SqlQuery $SqlQuery -logfile $logfile
    $rdrRowCnt = 0                                          
                    while($sqloutqryreader.Read()) 
                        {   
                            $rdrRowCnt++

                            if($rdrRowCnt -gt 1)
                                {
                                    Add-Content -Value "$TimeinSec Ambiguity: two Records are found for the current job: $jobname in the $mastTableNm table, hence aborting" -Path $logfile
                                    exit
                                }
                            $InputFileLst = $sqloutqryreader["InputFiles"].Split(";")
                            $OutputFileLst = $sqloutqryreader["OutputFiles"].Split(";")
                            Add-Content -Value "$TimeinSec Log: Input files:$InputFileLst and outputfiles:$OutputFileLst found for the current job: $jobname in the $mastTableNm table" -Path $logfile
                        }   
3
  • Does $sqloutqryreader.Read() return $true or 1 (or any other output that is not 0 \ $null \ $false)? Else while($sqloutqryreader.Read()) won't happen. Commented Jan 6, 2020 at 11:55
  • @T-Me its returning empty to log file which is $null i guess. I am not sure why it is returning a value other than true or false Commented Jan 6, 2020 at 12:41
  • @T-Me $sqloutqryreader.HasRows too is returning empty Commented Jan 6, 2020 at 12:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.