sqlite - 在 Windows 7x64 上使用 PowerShell 中的 SQLite?

标签 sqlite powershell windows-7-x64

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

# 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 组装结果:

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



尝试加载 x86 组装结果:

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)并告诉我。

SQLite PowerShell 模块的示例代码:

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 的文件中,并将其放在您的 module path 中的某个位置。 .然后放置你have downloaded的两个System.Data.SQLite.dll到子文件夹中,每个文件夹都在正确的文件夹中(x86 或 x64)。然后,在 PowerShell 中,键入:
Import-Module sqlite

然后实际加载程序集:
Add-SqliteAssembly

现在,您的初始代码(减去 loadfrom 的东西)应该可以工作了。

关于sqlite - 在 Windows 7x64 上使用 PowerShell 中的 SQLite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4552588/

相关文章:

java - Powershell 打开窗口(来自 Java.Runtime.exec)

powershell - 如何使用 Powershell 将哈希表与另一个哈希表进行比较?

visual-studio-2010 - 在 Windows 7 64 位上编译 OpenCV

sqlite - HTML5 : Can an SQlite database be specified in a manifest's Cache section?

SQLite 外键不匹配

sqlite - nhibernate 事务和单元测试

python - 创建空白字段并使用 sqlite、python 接收 INTEGER PRIMARY KEY

pdf - powershell - 检查pdf是否加密

java - Tomcat 中的 HttpServletRequest.getRemoteAddr() 返回 IPv6 格式的 IP 地址

c++ - 我正在尝试学习如何正确分离类头和代码