C# 如何使用函数从sqlite数据库中获取数据?

C# 如何使用函数从sqlite数据库中获取数据?,c#,android,sqlite,xamarin,xamarin.forms,C#,Android,Sqlite,Xamarin,Xamarin.forms,我正在用Xamarin表单制作一个应用程序,但不管我怎么做,我都无法让sqlite数据库正常工作。我想选择所有类别,其中menu_ID=1我该怎么做?我需要在另一个页面(CategoriePage.xaml.cs)中包含此代码,有人能帮我吗 以下是我使用的一些代码: (表3.cs): DB助手(TheMapDB.cs): 提前感谢您,我建议您使用如下函数创建一个DAO类:(或将此函数放在mapdb.cs中) public List GetCategoryByID(int-menuID) { 返回

我正在用Xamarin表单制作一个应用程序,但不管我怎么做,我都无法让sqlite数据库正常工作。我想选择所有类别,其中menu_ID=1我该怎么做?我需要在另一个页面(CategoriePage.xaml.cs)中包含此代码,有人能帮我吗

以下是我使用的一些代码:

(表3.cs):

DB助手(TheMapDB.cs):


提前感谢您,

我建议您使用如下函数创建一个DAO类:(或将此函数放在mapdb.cs中)

public List GetCategoryByID(int-menuID)
{
返回db.Table().Where(x=>x.menu_ID==menuID.ToList();
}
然后,您可以在DAO中的任何地方调用此函数。对我来说,这似乎是最好的解决办法。 当您将此函数放在类TheMapDB.cs中时,您可以在CategoryPage.xaml.cs中说:

 public partial class CategoriePage : ContentPage
    {
        static TheMapDB database;
        TheMapDB categorie = new TheMapDB();

        public CategoriePage(String txt)
        {
            InitializeComponent();
            var layout = new StackLayout { Padding = new Thickness(5, 10) };
            this.Content = layout;
            if(txt.Equals("1"))
            {
                txt = "this text is number 1";
                //needed code can't find soluction

            }
            var label = new Label { Text = txt, TextColor = Color.FromHex("#77d065"), FontSize = 20 };
            layout.Children.Add(label);
        }
    }
GetCategoryByID(menuID)


Thx这可能会起作用现在我只有一个数据库错误,但我会提出一个新问题。嗨,伙计,我有一个小问题,我如何使用这个函数来恢复我得到的所有类别的名称(名称)?返回db.Table().Where(x=>x.name.Equals(“nameOfCategory”).ToList();这就是您正在查找的内容吗?不,我指的是这样的内容:var categories=App.Database.GetCategoryByMenuID(1);listView.ItemsSource=categories.Name;(顺便说一句,这不起作用)所以我可以对不同的结果使用相同的函数。对不起,你的问题对我来说不是很清楚。也许你应该问一个问题并评论一个链接。
public class TheMapDB
{
    private SQLiteConnection db;

    public TheMapDB()
    {
        //Getting conection and Creating table  
        db = DependencyService.Get<ISQLite>().GetConnection();
        db.CreateTable<Categories>();
        db.CreateTable<Places>();
        db.CreateTable<Events>();

        var categories = new Categories()
        {
            ID = 1,
            Menu_ID = 1,
            Name = "test"
        };
        db.Insert(categories);   // Insert the object in the database
    }


    public IEnumerable<Categories> GetCategories()
    {
        return (from t in db.Table<Categories>() select t).ToList();
    }

    //Get specific Categorie
    public Categories GetCategorie(int id)
    {
        return db.Table<Categories>().FirstOrDefault(t => t.ID == id);
    }

    //Delete specific Categorie
    public void DeleteCategorie(int id)
    {
        db.Delete<Categories>(id);
    }

    //Add new student to Categorie
    public void AddCategorie(Categories categorie)
    {
        db.Insert(categorie);
    }
}
}
 public partial class CategoriePage : ContentPage
    {
        static TheMapDB database;
        TheMapDB categorie = new TheMapDB();

        public CategoriePage(String txt)
        {
            InitializeComponent();
            var layout = new StackLayout { Padding = new Thickness(5, 10) };
            this.Content = layout;
            if(txt.Equals("1"))
            {
                txt = "this text is number 1";
                //needed code can't find soluction

            }
            var label = new Label { Text = txt, TextColor = Color.FromHex("#77d065"), FontSize = 20 };
            layout.Children.Add(label);
        }
    }
public List<Category> GetCategoryByID(int menuID)
{
    return db.Table<Category>().Where(x => x.menu_ID == menuID).ToList();
}