Sql的小报告在导出到csv文件时以不正确的顺序显示列

时间:2014-03-20 15:24:43

标签: .net powershell sqldatareader

我正在Power shell中编写一个基本脚本来学习我正在进行的部分招聘项目。我目前是一名.NET开发人员,但他正在开发新的power shell。实际上昨天刚开始。无论如何,任何人都可以看一下这个脚本,让我知道为什么输出文件中列的顺序与我们的sql查询的正确顺序不匹配?

概念是使用每行的对象将其读入数组。任何帮助将不胜感激。谢谢!

以下是下面的脚本,减去连接内容。

########################################
#
#  USED TO GET RESULTS AND EXPORT TO SQL 
#
#
########################################



################################################
#   Lets first make sure we have the dates before we do anything else
################################################

$argsLength = $args.Length;

if($argsLength -lt 2)
{

  Write-Host "You must suply a date range in the format (from, to)."
  Write-Host "Example: PS> [command] from to : format [yyyy-mm-dd]";

}
else
{

   ###########################
   # Get the dates supplied by as params ###
   ###########################
   $from = $args[0];
   $to = $args[1];


   ###########################
   # Notify user job is now running
   ###########################
   Write-Host "Building report for dates " $from " to " $to "..."

   ###########################
   #Write-Host 'Get connection to SQL database'
   ###########################
   Try 
   {
     $Connection = New-Object System.Data.SQLClient.SQLConnection
     $Connection.ConnectionString = 
     $Connection.Open()
     $Command = New-Object System.Data.SQLClient.SQLCommand
     $Command.Connection = $Connection          

     $Connection2 = New-Object System.Data.SQLClient.SQLConnection
     $Connection2.ConnectionString = 
     $Connection2.Open()
     $Command2 = New-Object System.Data.SQLClient.SQLCommand
     $Command2.Connection = $Connection2 

   }
   Catch 
   {
     Throw "Can't connect to database..."
   }    

   ###########################
   #Write-Host 'Execute query'      
   ###########################
   $Command.CommandText = "SELECT job_number, job_desc, permit_number, pieces, rate, postage_total FROM monticello_charges WHERE (insert_date >= '$from') AND (insert_date < '$to') ORDER BY job_number"
   $Command2.CommandText = "SELECT job_number, description, permit_number, pieces, rate, total FROM mailshop_charges WHERE (mailing_date >= '$from') AND (mailing_date < '$to')ORDER BY permit_number, job_number"

   $Reader = $Command.ExecuteReader() 
   $Reader2 = $Command2.ExecuteReader()  

   ##### Do not add entries that have a [permit_number] of (360) and [rate] less than (0) ### #
   $results = @()
   while ($Reader.Read())
   {
        $row = @{}        
        for ($i = 0; $i -lt $Reader.FieldCount; $i++)
        {
            if($reader.GetValue(2) -eq 360 -and $reader.GetValue(4) -lt 0)
        {
              #pass over it
            }
            else
            {
               $row[$reader.GetName($i)] = $reader.GetValue($i)
            }             
        }
        $results += new-object psobject -property $row 
   }


   ##### Build the second table and replace 280 permits to (M) ###
   $results2 = @()
   while ($Reader2.Read())
   {       
        $row = @{}
        for ($i = 0; $i -lt $Reader2.FieldCount; $i++)
        {
            if($i -eq 2 -and $reader2.GetValue($i) -eq 280)
        {
              $row[$reader2.GetName($i)] = "M"
              $counter++
            }
            else
            {
               $row[$reader2.GetName($i)] = $reader2.GetValue($i)
            }             
        }
        $results2 += new-object psobject -property $row            
   }


   $finalResult = $results + $results2;

   $finalResult = $finalResult | sort-object @{Expression={$_.job_number}; Ascending=$true}

   $finalResult = $finalResult | sort-object @{Expression={$_.permit_number}; Ascending=$true}



    $finalResult | export-csv .\out.csv

    Write-Host "Report has been exported to current directoy. Filename:[out.csv]"
}

1 个答案:

答案 0 :(得分:1)

那是因为您正在使用哈希表,并且哈希表可能不会按照它们声明的相同顺序进行枚举。如果你有Powershell V3,你可以使用[ordered]类型加速器来创建一个有序的哈希表,它将保持与创建键的顺序相同:

$row = [ordered]@{} 

或者您可以在对象集合($ finalresult)上使用Select-Object在导出之前设置所需的顺序。