在 Windows 7x64 上使用 PowerShell 中的 SQLite? [英] Using SQLite from PowerShell on Windows 7x64?

查看:65
本文介绍了在 Windows 7x64 上使用 PowerShell 中的 SQLite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从 Windows 7 x64 中的 PowerShell 加载 System.Data.SQLite.dll 时遇到了困难.

I'm having a difficult time trying to load System.Data.SQLite.dll from PowerShell in Windows 7 x64.

# x64
[void][System.Reflection.Assembly]::LoadFrom("C:\projects\PSScripts\lib\System.Data.SQLite.x64.DLL")
# x86
#[void][System.Reflection.Assembly]::LoadFrom("C:\projects\PSScripts\lib\System.Data.SQLite.DLL")

$conn = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$conn.ConnectionString = "Data Source=C:\temp\PSData.db"
$conn.Open()
$command = $conn.CreateCommand()
$command.CommandText = "select DATETIME('NOW') as now, 'Bar' as Foo"
$adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void]$adapter.Fill($dataset)

尝试打开与 x64 程序集的连接会导致:

Trying to open the connection with the x64 assembly results in:

使用0"调用Open"的异常论点:试图加载一个不正确的程序格式.(来自 HRESULT 的例外:0x8007000B)"

Exception calling "Open" with "0" argument(s): "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

尝试加载 x86 程序集会导致:

Trying to load the x86 assembly results in:

使用1"个参数调用LoadFrom"的异常:无法加载文件或程序集'file:///C:\projects\PSScripts\lib\System.Data.SQLite.DLL' 或其依赖项之一.一个试图加载格式不正确的程序."

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\projects\PSScripts\lib\System.Data.SQLite.DLL' or one of its dependencies. An attempt was made to load a program with an incorrect format."

有什么想法或想法吗?

推荐答案

您的 x64 二进制文件是否可能已损坏?我可以使用以下代码在新下载的 system.data.sqlite.dll 副本上成功使用 add-type,并且我可以实例化所有相关对象.我还可以无错误地打开数据库,并成功执行查询.在您的数据库中尝试这种技术(本质上,使用 Add-Type 而不是 LoadFrom)并告诉我.

Is it possible that your x64 binary is corrupt? I'm able to successfully use add-type on a freshly downloaded copy of system.data.sqlite.dll using the below code, and I can instantiate all of the related objects. I'm also able to open a database without errors, and successfully execute a query. Try this technique (essentially, using Add-Type instead of LoadFrom) with your db and let me know.

SQLite PowerShell 模块的示例代码:

Sample code for a SQLite PowerShell module:

function Add-SqliteAssembly {
    # determine bitness (32 vs. 64) of current PowerShell session
    # I'm assuming bitness = system architecture here, which won't work on IA64, but who cares
    switch ( [intptr]::Size ) {
        4   { $binarch = 'x86' }
        8   { $binarch = 'x64' }
    }
    $modPath = $MyInvocation.MyCommand.Module.ModuleBase
    $SQLiteBinName = 'System.Data.SQLite.dll'
    $SQLiteBinPath = "$modPath\$binarch\$SQLiteBinName"
    Add-Type -Path $SQLiteBinPath 
}

要使用此模块,请将其保存到名为 sqlite.psm1 的文件中,并将其放置在 模块路径.然后将您已下载的两个 System.Data.SQLite.dll 放入子文件夹中,每个子文件夹都在适当的位置文件夹(x86 或 x64).然后,在 PowerShell 中,键入:

To use this module, save it to a file called sqlite.psm1 and place it somewhere in your module path. Then place the two System.Data.SQLite.dll which you have downloaded into sub-folders, each in the proper folder (x86 or x64). Then, in PowerShell, type:

Import-Module sqlite

然后实际加载程序集:

Add-SqliteAssembly

现在,您的初始代码(减去 loadfrom 内容)应该可以工作了.

Now, your initial code (minus the loadfrom stuff) should work.

这篇关于在 Windows 7x64 上使用 PowerShell 中的 SQLite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆