2016-07-04 16 views
0

我正在嘗試從PowerShell腳本加載SQLite.dll,因此我可以訪問本地SQLite數據庫,但我一直在[System.Reflection.Assembly]::LoadFrom()方法上收到異常。LoadFrom在加載SQLite程序集時失敗

錯誤說:

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or 
assembly 'file:///C:\Source\System.Data.SQLite.dll' or one of its dependencies. 
An attempt was made to load a program with an incorrect format." 
At C:\Source\getAppData.ps1:10 char:5 
+  [void][System.Reflection.Assembly]::LoadFrom($sqlite_library_path) 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : BadImageFormatException

我嘗試了好幾種不同版本的System.Data.SQLite.dll的 - 我的第一反應是,這是一個32位版本和我在64位操作系統。

我也曾嘗試Add-Type卻得到了一個類似的錯誤:

Add-Type : Could not load file or assembly 
'file:///C:\Source\System.Data.SQLite.dll' or one of its dependencies. An 
attempt was made to load a program with an incorrect format. 
At C:\Source\getAppData.ps1:12 char:5 
+  Add-Type -Path $sqlite_library_path 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [Add-Type],BadImageFormatException 
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand

這裏是PowerShell腳本;我確定它以管理員身份運行。我不確定是否需要解鎖DLL,授予權限,或者使用其他一些加載程序集的方法 - 對於PowerShell,我是相當新的,所以我認爲這是某種新手錯誤或環境問題,因爲我有朋友說這種方法應該工作。

[cmdletbinding()] 
[string]$sqlite_library_paxcth = "C:\Source\System.Data.SQLite.dll" 
[string]$db_query = "SELECT Name from kiosk where STRFTIME('%s',LastFullSync)>STRFTIME('%s',date('now', '-60 day'))" 
[string]$db_data_source = "C:\Source\AppData.db" 

if (!(Test-Path $sqlite_library_path)) {throw $outcome = "Unable to find sqlite library"} 
if (!(Test-Path $db_data_source)) {throw $outcome = "Unable to find appdb"} 

[void][System.Reflection.Assembly]::LoadFrom($sqlite_library_path) 

#Add-Type -Path $sqlite_library_path 

$db_dataset = New-Object System.Data.DataSet 
$db_data_adapter = New-Object System.Data.SQLite.SQLiteDataAdapter($db_query,"Data Source=$db_data_source") 
[void]$db_data_adapter.Fill($db_dataset) 

我的目標是簡單地查詢本地文件系統中的SQLite數據庫,並將結果傳遞給數據網格或其他GUI以顯示結果。

+1

您使用哪個.net框架版本? –

+1

您正在運行哪個版本的PowerShell?你還使用64位PowerShell或32位?通用快捷方式默認爲64位,因此請查找PowerShell(x86)。看看[this](http://stackoverflow.com/questions/8588960/determine-if-current-powershell-process-is-32-bit-or-64-bit)。 – Matt

+0

檢查'$ PSVersionTable.CLRVersion'和'$ env:PROCESSOR_ARCHITECTURE'的值,並確保您擁有該版本和操作系統體系結構的正確DLL。 –

回答

1

我有同樣的問題,並最終鑿擊我的嘗試去64位。使用32位版本的PowerShell和SQLite Assembly可以很好地滿足我的需求。

附註:我的筆記本電腦毫無疑問地加載了64位版本。我不知道爲什麼,這讓我有點慌張。 :)

相關問題