在 Windows 7x64 上通过 PowerShell 使用 SQLite?

发布于 2024-10-09 05:56:48 字数 1085 浏览 0 评论 0原文

我在尝试从 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 程序集的连接会导致:

使用“0”调用“Open”时出现异常 论据:“有人试图 加载一个不正确的程序 格式。 (HRESULT 的异常: 0x8007000B)"

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

使用“1”个参数调用“LoadFrom”时出现异常:“无法加载文件或程序集 'file:///C:\projects\PSScripts\lib\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)

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

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

Trying to load the x86 assembly results in:

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."

Any thoughts or ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城缀染半城烟沙 2024-10-16 05:56:48

您的 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 的文件中,并将其放置在 模块路径。然后将下载的两个System.Data.SQLite.dll放入子文件夹中,每个子文件夹位于正确的位置文件夹(x86 或 x64)。然后,在 PowerShell 中键入:

Import-Module sqlite

然后实际加载程序集:

Add-SqliteAssembly

现在,您的初始代码(减去 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.

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 
}

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

And then to actually load the assembly:

Add-SqliteAssembly

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

驱逐舰岛风号 2024-10-16 05:56:48

您需要确保 System.Data.SQLite.dll 对于位(32 或 64)和 .Net 版本都是正确的。
对于 Win 7 x64,默认安装的 .Net 是 3.5。即使您已经安装了 4.0(或更高版本)客户端,Powershell 也将使用 .Net 3.5。如果您跳过其他环节(在别处讨论),则可以在 Powershell 中使用 .NET 4.0。

从以下位置下载 ADO SQLite 包:
http://system.data.sqlite.org/index .html/doc/trunk/www/downloads.wiki

查找适用于 64 位 Windows 的预编译二进制文件 (.NET Framework 3.5 sp1)。选择适当的 zip(带或不带 VC++ 运行时)。
不要让文件名中的 2008(或 2010)欺骗了您。它是对用于编译的VC++版本的引用。该项目是当前的。截至撰写本文时,版本为 1.0.79.0,日期为 2012 年 1 月 28 日。不幸的是,此软件包中没有提供帮助文件。有用的 SQLite.Net.chm 文件位于 docs\ 文件夹中单独的包中。

解压 zip 后(无需安装或注册表破解),使用以下命令将 powershell 脚本指向 dll:

Add-Type -Path "\System.Data.SQLite.dll"

示例:
Add-Type -Path "C:\sql\sqliteFx35x64\System.Data.SQLite.dll"

其余的测试代码应该可以工作

You need to make sure that the System.Data.SQLite.dll is correct for both bit (32 or 64) and .Net version.
For Win 7 x64 the default installed .Net is 3.5. Powershell will use .Net 3.5 even if you have installed the 4.0 (or higher) clients. You can use .NET 4.0 in Powershell if you jump through other hoops (discussed elsewhere).

Download ADO SQLite package from:
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

Look for Precompiled Binaries for 64 bit Windows (.NET Framework 3.5 sp1). Select the appropriate zip (with or without the VC++ runtime).
Don't let the 2008 (or 2010) in the file name fool you. It's a reference to the VC++ version used to compile. The project is current. The version as of this writing is 1.0.79.0 dated Jan 28, 2012. Unfortunately the help file does not come with this package. The useful SQLite.Net.chm file is in the separate source package in the docs\ folder.

Once the zip is extracted (no install or registry hacks required), point your powershell script to the dll using:

Add-Type -Path "<drive><path>\System.Data.SQLite.dll"

Example:
Add-Type -Path "C:\sql\sqliteFx35x64\System.Data.SQLite.dll"

The rest of your test code should work

征﹌骨岁月お 2024-10-16 05:56:48

我会尝试在应用程序主目录中创建 YourApp.exe.config 文件(powershell.exe 中的 powershell.exe.config)如果是 PowerShell,请注意 x86 和 x64 的位置不同):

<?xml version="1.0"?>
<configuration>
 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
   <probing privatePath="C:\projects\PSScripts\lib"/>
  </assemblyBinding>
 </runtime>
</configuration>

如果没有帮助,请仔细检查所有 SQLite DLL 是否已正确安装。请参阅手册(我自己对 SQLite ADO .NET 提供程序不太了解)。

I would try to create the YourApp.exe.config file in the application home directory (powershell.exe.config in the powershell.exe's directory if it is PowerShell, mind that the location is different for x86 and x64):

<?xml version="1.0"?>
<configuration>
 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
   <probing privatePath="C:\projects\PSScripts\lib"/>
  </assemblyBinding>
 </runtime>
</configuration>

If it does not help then double check that all the SQLite DLLs are installed properly. See the manual (I do not know much about SQLite ADO .NET provider myself).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文