Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Xamarin.Forms上的SQLite PCL数据库中删除项?_C#_Database_Sqlite_Xaml_Xamarin - Fatal编程技术网

C# 如何从Xamarin.Forms上的SQLite PCL数据库中删除项?

C# 如何从Xamarin.Forms上的SQLite PCL数据库中删除项?,c#,database,sqlite,xaml,xamarin,C#,Database,Sqlite,Xaml,Xamarin,我正在使用一个Xamarin.Forms的SQLite数据库部署教程来学习如何从移动数据库中添加、获取和删除项目。目前,我不太确定如何从表中的数据库项中检索Id属性以通过deleteData方法传递它。基本上,在我添加了一定数量的项目之后,我想删除一些数据。我在XAML绑定页面中实现了一个按钮,但我需要知道如何检索某些项的Id属性,并将它们传递到我创建的deleteData方法中。有人能帮我吗 以下是代码: StudentDB.cs(数据库类) Register.xaml.cs(创建操作数据的方

我正在使用一个Xamarin.Forms的SQLite数据库部署教程来学习如何从移动数据库中添加、获取和删除项目。目前,我不太确定如何从表中的数据库项中检索Id属性以通过deleteData方法传递它。基本上,在我添加了一定数量的项目之后,我想删除一些数据。我在XAML绑定页面中实现了一个按钮,但我需要知道如何检索某些项的Id属性,并将它们传递到我创建的deleteData方法中。有人能帮我吗

以下是代码:

StudentDB.cs(数据库类)

Register.xaml.cs(创建操作数据的方法的C#页面)

StudentList.xaml.cs(用数据库项填充列表视图的C#页面)


这取决于如何触发
deleteData
,但您可能需要使用
StudentListView.ItemSelected
,它应该是
Student
对象之一

using System;
using System.Linq;
using SQLite.Net;
using Xamarin.Forms;
using System.Collections.Generic;

namespace XamarinSqliteSample
{
public class StudentDB
{

    private SQLiteConnection _sqlconnection;

    public StudentDB ()
    {
        _sqlconnection = DependencyService.Get<ISQLite> ().GetConnection ();
        _sqlconnection.CreateTable<Student> ();
    }

    public IEnumerable<Student> GetStudents()
    {
        return (from t in _sqlconnection.Table<Student> ()
                select t).ToList ();
    }

    public Student GetStudent (int id)
    {
        return _sqlconnection.Table<Student> ().FirstOrDefault (t => t.Id == id);
    }

    public void DeleteStudent(int id)
    {
        _sqlconnection.Delete<Student>(id);
    }

    public void AddStudent(Student student)
    {
        _sqlconnection.Insert(student);
    }
}
}
using System;
using SQLite.Net.Attributes;

namespace XamarinSqliteSample
{
    public class Student
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }

        public Student ()
        {

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace XamarinSqliteSample
{
    public partial class Register : ContentPage
    {
        public StudentDB _studentdb;
        public Student student;

        public Register()
        {
            InitializeComponent();
        }

        public void adddata(object s, EventArgs args)
        {
            student = new Student();
            _studentdb = new StudentDB();
            student.Name = name.Text;
            student.Address = address.Text;
            student.Phone = phone.Text;
            student.Id++;
            _studentdb.AddStudent(student);
        }

        public void Showdata(object sender, EventArgs args)
        {
            Navigation.PushModalAsync(new StudentList());
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace XamarinSqliteSample
{
    public partial class StudentList : ContentPage
    {
        public StudentDB _database;
        public Student student;

        public StudentList()
        {
            InitializeComponent();

            _database = new StudentDB();
            var students = _database.GetStudents();
            StudentListView.ItemsSource = students;
        }

        public void deleteData(object s, EventArgs args)
        {
            _database = new StudentDB ();
            _database.DeleteStudent (//Id attribute is passed in here);
        }
    }