定期我得到以下异常:

无法加载DLL 'SQLite.Interop.dll':无法找到指定的模块。(异常来自HRESULT: 0x8007007E)

我使用的是1.0.82.0。在VS2010, Win7 64操作系统下使用nuget安装。

一旦异常开始出现,它就会不断出现——在调试和发布中,在VS内部或外部运行应用程序。

阻止它的唯一方法就是退出并重新登录。不抛出异常并加载dll。 它可以工作几天,但之后又会坏掉。

有人见过这样的情况吗,有解决方案吗?


当前回答

升级到Visual Studio 2019版。16.10导致了我的问题,其中msbuild报告了以下System.Data.SQLite.Core-package:

CopySQLiteInteropFiles:
Skipping target "CopySQLiteInteropFiles" because it has no outputs.

https://github.com/dotnet/msbuild/issues/6493

微软表示,该漏洞已被修复。16.10.4. 现在只需要等待AppVeyor更新他们的Visual Studio图像(在此之前,可以使用以前的Visual Studio 2019)。

现在,AppVeyor正在为当前和以前的Visual Studio 2019-image使用破碎的dotnet-build-engine。现在一个必须显式安装dotnet sdk ver。5.0.302:

Invoke-WebRequest -Uri 'https://dot.net/v1/dotnet-install.ps1' -UseBasicParsing -OutFile "$env:temp/dotnet-install.ps1"; & $env:temp\dotnet-install.ps1 -Architecture x64 -Version 5.0.302 -InstallDir "$env:ProgramFiles\dotnet"

其他回答

因此,在添加了NuGet之后,部署不会复制Interops。你可以把这个添加到你的csproj文件中,它应该会修复这个行为:

 <PropertyGroup> 
    <ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>
    <CopySQLiteInteropFiles>false</CopySQLiteInteropFiles>
    <CleanSQLiteInteropFiles>false</CleanSQLiteInteropFiles>
    <CollectSQLiteInteropFiles>false</CollectSQLiteInteropFiles>
 </PropertyGroup>

如果你看一下NuGet for SQLite的源代码,你可以看到这些具体在做什么。这允许我使用ASP进行部署。净的核心。

我正在做一个简单的控制台应用程序,向SQLite数据库添加一些测试数据,并得到这个错误。项目的配置是“Any CPU”。我通过将SQLite.Interop.dll复制到bin\debug文件夹来修复它。更好的方法是使用@Wil的方法,但是如何为“任何CPU”配置指定该方法呢?

这里确实有很多答案,但我的答案简单明了,没有gac。

问题是,可执行文件需要一个正确的SQLite.Interop.dll (x86或x64)的副本来访问我们的数据库。

大多数架构都有层,在我的例子中,数据层有SQLite连接所需的DLL。

所以我简单地把一个后构建脚本到我的数据层解决方案和一切工作良好。


TL; diana;

在构建选项中将解决方案的所有项目设置为x86或x64。 使用SQLite nuget包将以下Post-Build-Script添加到项目中: /y . xcopy "$(TargetDir)x64" "$(SolutionDir)bin\Debug\

当然,您必须更改发布构建和x86构建的脚本。


STL; diana;

将SQLite.Interop.dll放在*.exe文件旁边。

来自NuGet的多体系结构(x86, x64)版本SQLite的默认安装显示了您所描述的行为。如果你想加载。net运行时选择在你的机器上运行你的应用程序的实际体系结构的正确版本,那么你可以给DLL加载器一个关于在哪里找到正确库的提示,如下所示:

在Program.Main()之前添加kernel32.dll函数调用SetDLLDirectory()的声明:

    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
    [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
    static extern bool SetDllDirectory(string lpPathName);

然后使用您自己的方法确定正确的子目录,以找到'SQLite.Interop.dll'的特定于体系结构的版本。我使用以下代码:

    [STAThread]
    static void Main()
    {
        int wsize = IntPtr.Size;
        string libdir = (wsize == 4)?"x86":"x64";
        string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
        SetDllDirectory(System.IO.Path.Combine(appPath, libdir));

I don't know why this has not been included yet, but I had to do the research and find this out for myself, so hopefully someone will find this answer and be saved the trouble. This was for a WPF app. It worked fine on my Dev box, but did not work on the computer where I was copying it and got the Unable to load DLL 'SQLite.Interop.dll' error. I ported over all of its associated directories and files, directly from my "Debug" folder to this other computer when I got the same error as the OP when I ran it. My "bin" folder that contained my DLLs had been copied to "Debug\bin" and all were included, along with my application files when I did my copying to the other computer using this path, so it was not missing any files.

我在其他答案中看到的不适用的东西:

I did not use the NuGet package or need to create x86 or x64 folders that it seems that NuGet package creates. My DLLs (System.Data.SQLite and SQLite.Interop.dll, along with System.Data.SQLite.config) are in the "bin" folder in my project and were copied in manually (create "bin" folder in Solution Explorer in VS, paste DLLs into this folder in Windows Explorer, use Add > Existing Item to bring files into VS folder/project). Then I reference them as Referenced Assemblies in my project using that location ("References" > "Add Reference", and browse to one, rinse, repeat for the rest). This ensures my project knows exactly where they are. I did not need to reference any SQLite DLL file in my app.config or even touch my MyProject.csproj file. I did not even need to specify a particular processor! My project's build is for "Any CPU", even though I have only mixed or 64-bit DLLs and will only be running on Windows 7+, which are 64-bit OSes. (no x86-only/32-bit solely DLLs) I was already specifying them as "Content" and "copy if newer" for these DLLs when I experienced the OP's error.

我从https://system.data.sqlite.org/index.html/doc/trunk/www/faq.wiki#q20上找到了这个:

(11) Why do I get a DllNotFoundException (for "sqlite3.dll" or "SQLite.Interop.dll") when trying to run my application? Either the named dynamic link library (DLL) cannot be located or it cannot be loaded due to missing dependencies. Make sure the named dynamic link library is located in the application directory or a directory along the system PATH and try again. Also, be sure the necessary Visual C++ runtime redistributable has been installed unless you are using a dynamic link library that was built statically linked to it.

我要强调这段内粗体的部分。目标计算机是新的,除了. net 4.0之外没有加载任何程序。一旦我安装了c++,它就能够完成SQLite的命令。这应该是第一个常见问题之一,也是先决条件的一部分,但它被埋在了第11位。我的开发计算机已经加载了它,因为它带有Visual Studio,所以这就是它在那里工作的原因。

下载: Visual c++ Redistributable for Visual Studio 2015 https://www.microsoft.com/en-us/download/details.aspx?id=48145

更新3(累积更新): https://www.microsoft.com/en-us/download/details.aspx?id=53587