XAML System.DllNotFoundException:'SQLite.Interop.dll'

nbysray5  于 2023-08-01  发布在  SQLite
关注(0)|答案(1)|浏览(123)

我在.NET Maui中创建了一个ListView,在SQLite中创建了一个数据库。我想将我的应用程序连接到数据库,但得到:
System.DllNotFoundException:'SQLite.Interop.dll'

namespace MauiApp1;

using System.Data;
using System.Data.SQLite; 

public partial class MainPage: ContentPage
{
    public MainPage()
    {
        InitializeComponent();

    }
    //SQLiteConnection conn;
    //SQLiteCommand cmd;
    //List<int> lstc;
    private void btnsubmit_Clicked(object sender, EventArgs e)
{

        SQLiteConnection conn = new SQLiteConnection(@"Data Source=D:\Program Files\SQLiteStudio\listview");
        conn.Open();

        var query = "select * from history";
        SQLiteCommand cmd = new SQLiteCommand(query, conn);

        SQLiteDataReader reader = cmd.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(reader);

        List<int> lstc = new List<int>();

        while (reader. Read())
        {
            lstc.Add(dt.Rows.Count);

        }

        lstx.ItemsSource = lstc;
        conn.Close();
    }

}

字符串
我添加了超过5个NugetPackage,但无法解决它。

hpcdzsge

hpcdzsge1#

System.DllNotFoundException:'SQLite.Interop.dll'
此错误表示System.Data.Sqlite不包含本机互操作库。或者,您可以使用Microsoft.Data.Sqlite
我创建了一个虚拟数据库:hello.db来测试它。在我这边有效。下面是示例代码供您参考:

private void OnCounterClicked(object sender, EventArgs e)
{
      using (var connection = new SqliteConnection(@"Data Source=C:\Users\...\repos\MauiApp1\MauiApp1\hello.db"))
      {
            connection.Open();
            var command1 = connection.CreateCommand();
            command1.CommandText =
            @"
                    CREATE TABLE user (
                        id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                        name TEXT NOT NULL
                    );
                    INSERT INTO user
                    VALUES (1, 'Brice'),
                           (2, 'Alexander'),
                           (3, 'Nate');
                ";
            command1.ExecuteNonQuery();

            var command = connection.CreateCommand();
            command.CommandText =
            @"
                    SELECT name
                    FROM user
                   
                ";
   
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var name = reader.GetString(0);
                    System.Diagnostics.Debug.WriteLine($"Hello, {name}!");
                }
            }
            connection.Close(); 
      }  
}

字符串
有关更多详细信息,请参阅https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/?tabs=netcore-cli

相关问题