Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows 7x64上使用PowerShell中的SQLite?_Sqlite_Powershell_Windows 7 X64 - Fatal编程技术网

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

在Windows 7x64上使用PowerShell中的SQLite?,sqlite,powershell,windows-7-x64,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\Syst

我在尝试从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”调用“打开”时发生异常 参数:“试图 用不正确的代码加载程序 格式。(HRESULT的例外情况: 0x8007000B)”

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

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


有什么想法吗?

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



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

您的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 stuff)应该可以工作了。

您需要确保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包:

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

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

添加类型-Path“\System.Data.SQLite.dll”

示例:
添加类型-路径“C:\sql\sqliteFx35x64\System.Data.SQLite.dll”


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

有趣的是,我昨天正在处理一个类似的问题。我确实克服了您看到的错误,但仍然无法打开数据库。我现在正试图把我所知道的打包成一个答案。我还没有尝试过你的模块方法,但是你的坏二进制让我思考。我相信我正在解决一个损坏的x64程序集和一个会话问题,因为一旦加载/尝试加载程序集,无论您做了什么,结果都是一样的。但是,如果重新启动Powershell ISE/Power Gui脚本编辑器,则可以加载/使用x64程序集而不会出现问题。因此,dll的一个新副本和在更改之间重新启动编辑器使得我可以查询SQLite。谢谢你的帮助杰森
Add-SqliteAssembly