Xamarin形式的本地SQLite数据库

问题描述 投票:0回答:1

因此,我使用“ SQLite的数据库浏览器”],将数据库外部 Xamarin表单作为示例,并将其放在资产文件

EXAMPLE DATABASE

并且我正在尝试学习如何将数据库连接到数据库,所以我做到了这一点:

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Behoerdensprache.Pages.FinanzAmt">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand"
                   x:Name="LabelControl"/>
            <Entry Placeholder="Search" x:Name="SearchEntry"/>
            <Button Text="Press me" Clicked="Button_Clicked"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

C#ButtonCode:

    private void Button_Clicked(object sender, EventArgs e)
    {
        SQLiteConnection conn = new SQLiteConnection(App.DataBaseLocation);
        conn.CreateTable<Wortschatz>();
        var gets = conn.Table<Wortschatz>().Where(i => i.Wort == SearchEntry.Text).ToString();
        LabelControl.Text = gets;

        conn.Close();

    }

App.DataBaseLocation

在Mainactivity和AppDelegate中定义:

在主要活动中:

        string dbName = "BeamtenSprache.sqlite";
        string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string fullPath = Path.Combine(folderPath, dbName);
        LoadApplication(new App(fullPath));

在AppDelegate中:

        string dbName = "wortschatz.sqlite";
        string folderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", "Library");
        string fullPath = Path.Combine(folderPath, dbName);
        LoadApplication(new App(fullPath));

new App(fullPath)

我在App.cs中定义的这部分:
 public static string DataBaseLocation = string.Empty;
    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }

    public App(string databaseLocation)
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
        DataBaseLocation = databaseLocation;
    }

但是当我运行该应用程序并按下按钮时,此显示为:

ON_ANDROID

因此,我在Xamarin Forms之外使用“ SQLite的数据库浏览器”在Xamarin Forms外部创建了一个示例数据库,并将其放在Assets File中,我试图学习如何将数据库连接到它,所以我做到了:XAML:

database sqlite xamarin xamarin.forms connection-string
1个回答
0
投票
[xamarin.forms.samples中有一个Todo示例项目,显示了如何在本地SQLite数据库中存储和访问数据。

Android

© www.soinside.com 2019 - 2024. All rights reserved.