加载SQLite程序集时LoadFrom失败

时间:2016-07-04 20:13:39

标签: c# .net sqlite powershell

我正在尝试从PowerShell脚本加载SQLite.dll,因此我可以访问本地SQLite数据库,但我一直在[System.Reflection.Assembly]::LoadFrom()方法上获得异常。

错误说:

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 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并最终破坏了我的64位尝试。使用32位版本的PowerShell和SQLite程序集可以满足我的需求。

旁注:我的笔记本电脑加载64位版本没有任何疑虑。我不确定为什么,这让我有点慌张。 :)