无法加载 DLL 'e_sqlite3': 找不到指定的模块。

9
我是一名帮助翻译的助手。

我有一个Xamarin Forms解决方案。我将sqlite-net-pcl添加为所有项目的参考。它在Android上运行良好,但在Windows 8.1和Windows Phone 8.1上崩溃。我有一个IOS项目,但目前没有OSX来尝试它。

我在Windows项目中使用这个来访问数据库:

using System.IO;
using SQLite;
using Xamarin.Forms;
using HelloXamarin.Windows;
using Windows.Storage;

[assembly: Dependency(typeof(SQLiteDb))]

namespace HelloXamarin.Windows
{
   public class SQLiteDb : ISQLiteDb
    {
        public SQLiteAsyncConnection GetConnection(string databaseName)
        {
            var documentsPath = ApplicationData.Current.LocalFolder.Path;
            var path = Path.Combine(documentsPath, databaseName);
            return new SQLiteAsyncConnection(path);
        }
    }
}

这是我的参考资料:

References

当我尝试访问数据库时,出现以下异常:

类型“SQLite.SQLiteConnection”的初始值设定项引发了异常。

无法加载 DLL“e_sqlite3”:找不到指定的模块。(HRESULT 异常:0x8007007E)

  • 位于 SQLitePCL.SQLite3Provider_e_sqlite3.NativeMethods.sqlite3_win32_set_directory(UInt32 directoryType, String directoryPath)
  • 位于 SQLitePCL.SQLite3Provider_e_sqlite3..ctor()
  • 位于 SQLitePCL.Batteries_V2.Init() 位置: SQLite.SQLiteConnection..cctor()

我不知道如何解决这个问题,请帮帮我!

整个解决方案可以在 GitHub 上找到: https://github.com/apspot/HelloXamarin

4个回答

8
对我来说,它的工作方式是将 e_sqlite3 bundle 添加到可执行项目中。

1
在我的 Mac 上它可以工作。谢谢。 - dani herrera
你怎么做到的? - Shahryar Saljoughi
从这里获取:https://www.nuget.org/packages/SQLitePCLRaw.bundle_e_sqlite3 - undefined

3

现在这个问题仍然是未解决的。因此,在他们提供一个可靠的解决方案之前,您可以使用这个解决方法来暂时解决该问题。

添加一个帮助类

using System;
using System.Diagnostics;
using System.IO;

namespace SQLitePCL
{
    public class NativeLibraryHack
    {
        public static bool Hacked { get; private set; }

        public static bool DoHack()
        {
            if (Hacked) return true;

            try
            {
                const string runtimeFolderName = "/runtimes";

                var destinationPath = typeof(SQLitePCL.raw).Assembly.Location
                    .Replace("\\", "/");
                var destinationLength = destinationPath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase);
                var destinationDirectory = destinationPath.Substring(0, destinationLength) + runtimeFolderName;

                var sourcePath = new Uri(typeof(SQLitePCL.raw).Assembly.CodeBase)
                    .AbsolutePath;
                var sourceLength = sourcePath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase);
                var sourceDirectory = sourcePath.Substring(0, sourceLength) + runtimeFolderName;

                if (Directory.Exists(sourceDirectory))
                    CopyFilesRecursively(new DirectoryInfo(sourceDirectory), new DirectoryInfo(destinationDirectory));
            }
            catch (Exception ex)
            {
                //Ignore Exception
                Debug.WriteLine(ex.Message);
                return false;
            }

            return (Hacked = true);
        }

        private static void CopyFilesRecursively(
            DirectoryInfo source,
            DirectoryInfo target
        )
        {
            foreach (var dir in source.GetDirectories())
                CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));

            foreach (var file in source.GetFiles())
            {
                try
                {
                    var destinationFile = Path.Combine(target.FullName, file.Name);
                    if (!File.Exists(destinationFile))
                        file.CopyTo(destinationFile);
                }
                catch (Exception ex)
                {
                    //Ignore Exception
                    Debug.WriteLine(ex.Message);
                }
            }
        }
    }
}

在您的数据库迁移脚本之前添加hack,我正在使用Web API 2,所以我在RouteConfig.RegisterRoutes上进行了操作。
    NativeLibraryHack.DoHack();

    using (KSDBContext db = new KSDBContext())
    {
        db.Database.Migrate();                 
    }

它对我不起作用。你有哪个 SQLite 连接字符串? - tuke307
1
在许多其他方法都行不通之后,这个对我很有效。 - Peter

1

您需要添加SQLite扩展。

  1. 转到工具 > 扩展和更新
  2. 转到在线,然后搜索SQLite。
  3. 下载适用于Windows Runtime的SQLite。

enter image description here

  1. 在您的Windows项目中,添加引用并确保您添加了该扩展。

enter image description here

同时删除 Microsoft.VCLibs 的引用。

我已经添加了SQLite for Windows Runtime,但问题仍然存在:链接 - Péter Aradi
你是否也引用了Visual Studio Runtime C++ 2013库? - Adam
如果你指的是“Microsoft Visual C++ 2013 Runtime Package...”,那么它已经被添加为引用,你可以在这个截图中看到它。 - Péter Aradi

0

尝试引用Visual C++ 2015 Runtime for Universal Windows Platform Apps。这对我解决了问题。

  1. 进入“引用”
  2. 添加引用
  3. 扩展
  4. 勾选“Visual C++ 2015 Runtime for Universal Windows Platform Apps”
  5. 确定

谢谢你的回答。不幸的是,我现在无法检查它,因为我有Visual Studio许可证问题 :( - Péter Aradi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接