2011-04-26 48 views
1

3日编辑:为什么让在PowerShell脚本的差别到底这回?

以下两个脚本之间唯一的区别(除了函数的名字)是事实,第二不通过明确的返回返回结果。但他们表现不同。第一条显示两行

abc 
efg 
在网格

,而第二节目在网格行。

Skript 1:

ipmo WPK 

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True" 
$conn = new-object System.Data.SQLClient.SQLConnection 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

function Invoke-sql1 
{ 
    param([string]$sql, 
      [System.Data.SQLClient.SQLConnection]$connection 
      ) 
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection) 
    $ds = New-Object system.Data.DataSet 
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd) 
    $da.fill($ds) | Out-Null 
    return $ds.tables[0] 
} 

function Show-Bockmarks ($conn) { 
     New-ListView -Name ListView -View { 
      New-GridView -AllowsColumnReorder -Columns { 
       New-GridViewColumn "title" 
      } 
    } -show -On_Loaded { 
      $ff_sql = @" 
SELECT 'abc' title 
UNION 
SELECT 'efg' title 
"@ 
      $TableView = $window | Get-ChildControl ListView 
      $TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn) 
      } 
} 

Show-Bockmarks $conn 

脚本2:

ipmo WPK 

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True" 
$conn = new-object System.Data.SQLClient.SQLConnection 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

function Invoke-sql2 
{ 
    param([string]$sql, 
      [System.Data.SQLClient.SQLConnection]$connection 
      ) 
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection) 
    $ds = New-Object system.Data.DataSet 
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd) 
    $da.fill($ds) | Out-Null 
    $ds.tables[0] 
} 

function Show-Bockmarks ($conn) { 
     New-ListView -Name ListView -View { 
      New-GridView -AllowsColumnReorder -Columns { 
       New-GridViewColumn "title" 
      } 
    } -show -On_Loaded { 
      $ff_sql = @" 
SELECT 'abc' title 
UNION 
SELECT 'efg' title 
"@ 
      $TableView = $window | Get-ChildControl ListView 
      $TableView.ItemsSource = @(Invoke-sql2 -sql $ff_sql -connection $conn) 
      } 
} 

Show-Bockmarks $conn 

第一个脚本示出了在格栅2点的行,而第二显示到网格中的空行。 第二行为似乎与此类似

3脚本没有使用功能:在这里没有使用功能的

ipmo WPK 

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True" 
$conn = new-object System.Data.SQLClient.SQLConnection 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

      $ff_sql = @" 
SELECT 'abc' title 
UNION 
SELECT 'efg' title 
"@ 

function Show-Bockmarks ($conn) { 
     New-ListView -Name ListView -View { 
      New-GridView -AllowsColumnReorder -Columns { 
       New-GridViewColumn "title" 
      } 
    } -show -On_Loaded { 
     $TableView = $window | Get-ChildControl ListView 
     $cmd = new-object System.Data.SQLClient.SQLCommand($ff_sql,$conn) 
     $ds = New-Object system.Data.DataSet 
     $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd) 
     $da.fill($ds) | Out-Null 
     $TableView.ItemsSource = @($ds.tables[0].rows) 
    } 
} 

Show-Bockmarks $conn 

注意,我必须明确地使用$ ds.tables [0] 。行。否则,我得到的错误

Exception setting "ItemsSource": "Cannot convert the "Table" 
value of type "System.Data.DataTable" 
to type "System.Collections.IEnumerable"." 

PowerShell function won't return object或许可以解释,为什么这种行为类似功能,无需回报。但是,回报如何使2行显示在网格中?

原帖:

的功能调用,SQLite和调用,sqlite1几乎相同。

唯一的区别是,调用-sqlite的使用明确的回报。 的区别是非常微妙的,当我执行

$o1 = Invoke-sqlite $sql $conn 
$o2 = Invoke-sqlite2 $sql $conn 

我看不出有什么区别。但是,在下面的脚本的完整背景下,与调用-sqlite的网格填充数据和调用-sqlite的网格填充空行。

BTW:该脚本的目的是为1到3个关键字组合的书签搜索firefox places.sqlite历史数据库的副本。您必须修改为DLL线5的路径和SQLite数据库行路8

如果你有问题System.Data.SQLite.dll看到this

ipmo WPK 

if (! $sqlitedll) 
{ 
    $sqlitedll = [System.Reflection.Assembly]::LoadFrom("C:\Program Files\System.Data.SQLite\bin\System.Data.SQLite.dll") 
} 

$ConnectionString = "Data Source=C:\Var\sqlite_ff4\places.sqlite" 

$conn = new-object System.Data.SQLite.SQLiteConnection 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

# $sql = "SELECT * from moz_bookmarks t1 where parent = 4 and t1.title = 'sqlite' or t1.title = 'sql'" 

function Invoke-sqlite 
{ 
    param([string]$sql, 
      [System.Data.SQLite.SQLiteConnection]$connection 
      ) 
    $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection) 
    $ds = New-Object system.Data.DataSet 
    $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd) 
    $da.fill($ds) | Out-Null 
    return $ds.tables[0] 
} 

function Invoke-sqlite2 
{ 
    param([string]$sql, 
      [System.Data.SQLite.SQLiteConnection]$connection 
      ) 
    $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection) 
    $ds = New-Object system.Data.DataSet 
    $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd) 
    $da.fill($ds) | Out-Null 
    $ds.tables[0] 
} 

# $o1 = Invoke-sqlite $sql $conn 
# $o2 = Invoke-sqlite2 $sql $conn 


function Show-Bockmarks ($resource) { 
    #New-StackPanel -Orientation vertical { 
    New-Grid -Rows 2 -Columns 1 -width 1400 -hight 1000 { 

     New-StackPanel -Orientation horizontal -column 0 -row 0 -Children { 
      New-Label '1. Keyword' 
      New-TextBox -Name tag1 -width 200 
      New-Label '2. Keyword' 
      New-TextBox -Name tag2 -width 200 
      New-Label '3. Keyword' 
      New-TextBox -Name tag3 -width 200 
      New-Button -Name Search "search" -On_Click { 
      $text1 = $window | Get-ChildControl Tag1 
      $tag1 = $text1.Text 
      $text2 = $window | Get-ChildControl Tag2 
      $tag2 = $text2.Text 
      $text3 = $window | Get-ChildControl Tag3 
      $tag3 = $text3.Text 
      if ($tag2 -ne '') { 
$clause2 = @"    
    join moz_bookmarks l2 on b.fk = l2.fk and b.id <> l2.id 
    join moz_bookmarks t2 on l2.parent = t2.id and t2.parent = 4 and upper(t2.title) = upper('$tag2') 
"@       
      } else { $clause2 = '' }   

      if ($tag3 -ne '') { 
$clause3 = @"    
    join moz_bookmarks l3 on b.fk = l3.fk and b.id <> l3.id 
    join moz_bookmarks t3 on l3.parent = t3.id and t3.parent = 4 and upper(t3.title) = upper('$tag3') 
"@       
      } else { $clause3 = '' }   

$ff_sql = @" 
SELECT b.title, datetime (b.dateAdded/1000000, 'unixepoch', 'localtime') dateAdded , p.url 
    from moz_bookmarks b 
    join moz_bookmarks l1 on b.fk = l1.fk and b.id <> l1.id 
    join moz_bookmarks t1 on l1.parent = t1.id and t1.parent = 4 and upper(t1.title) = upper('$tag1') 
    join moz_places p on b.fk = p.id $clause2 $clause3 
where b.title is not null and b.type = 1 
"@ 
#    $query = $window | Get-ChildControl query 
#    $query.text = $ff_sql 
      $conn = $resource.conn 
      $window.Title = "$($conn.database) Database Browser" 
      $TableView = $window | Get-ChildControl TableView 
      $TableView.ItemsSource = Invoke-sqlite -sql $ff_sql -connection $conn 
      } 
#    New-textbox -Name query 
      New-Button -Name Cancel "Close" -On_Click {$window.Close()} 
     } 
     # -VerticalScrollBar $True 
     # New-ScrollViewer { 
     New-ListView -Column 0 -Row 1 -Name TableView -View { 
      New-GridView -AllowsColumnReorder -Columns { 
       New-GridViewColumn "title" 
       New-GridViewColumn "dateAdded" 
       New-GridViewColumn "url" 
      } 
     } -On_SelectionChanged { 
      start $this.selecteditem.url 
     } 
     #} 

    } -asjob -Resource $resource 
} 

Show-Bockmarks -resource @{conn = $conn} 

回答

0

为了帮助,我写了一篇关于SQLCLIENT顶部同样的事情,它似乎很好地工作。

function func1 
{ 
    param([string]$sql, 
     [System.Data.SQLClient.SQLConnection]$connection 
    ) 


    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$conn) 
    $ds = New-Object system.Data.DataSet 
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd) 
    $da.fill($ds) | Out-Null 

    $ds.Tables[0] 
} 

function func2 
{ 
    param([string]$sql, 
     [System.Data.SQLClient.SQLConnection]$connection 
    ) 


    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$conn) 
    $ds = New-Object system.Data.DataSet 
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd) 
    $da.fill($ds) | Out-Null 

    return $ds.Tables[0] 
} 

Clear-Host 

$sqldll = [reflection.assembly]::loadwithpartialname("System.Data.SqlClient") 
$connectionString="Data Source=JPBHPP2\SQLEXPRESS;Initial Catalog=Ardeche-Earth;Integrated Security=True" 

$conn = new-object System.Data.SQLClient.SQLConnection 
$sql = "SELECT * FROM communes WHERE COM_NCC LIKE 'AURI%'" 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

$a = func1 -sql $sql -connection $conn 
$b = func2 -sql $sql -connection $conn 

Write-Host "`$a : $($a.count)" 
Write-Host "`$b : $($b.count)" 

$conn.Close() 

$ a和$ b持有System.Data.DataRow

+0

同一阵列我没有看到使用SQLite任两个结果之间的差。只有在WPK的背景下,差异鞋。我使用System.Data.SQLClient重写了我的脚本。与WPK的背景差异仍然存在。 – 2011-04-27 07:12:16

相关问题