Powershell访问SQL Server数据库查询返回结果集

2019-06-19 20:04栏目:技术创新
TAG:

function Invoke-SQL { 
  param( 
  [Parameter(Mandatory=$true)] 
  $sql = "select * from Kunden", 
  [Parameter(Mandatory=$true)] 
  $connectionstring 
  ) 

  $db = New-Object -comObject ADODB.Connection 
  $db.Open($connectionstring) 
  $rs = $db.Execute($sql) 
  while (!$rs.EOF) { 
    $hash = @{} 
    foreach ($field in $rs.Fields) { 
      $hash.$($field.Name) = $field.Value 
    } 
    $rs.MoveNext()     
    New-Object PSObject -property $hash
  } 
  $rs.Close() 
  $db.Close() 
}
#该片段来自于http://outofmemory.cn

版权声明:本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:Powershell访问SQL Server数据库查询返回结果集