I am using one of the Xamarin.Forms tutorials on SQLite database deployment to learn how to add, get and delete items from a mobile database. At the moment, I am not quite sure how to retrieve a the Id attribute from a database item in my table to pass it through the deleteData method. Basically, after I add in a certain amount of items, I would like to delete some of the data. I have implemented a button in my XAML binding pages but I need to know how to retrieve the Id attribute of certain items and pass them into the deleteData method I have created. Can anyone help me with this?
Here are the codes:
StudentDB.cs (database class)
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);
}
}
}
Student.cs (item attributes for database table)
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 ()
{
}
}
}
Register.xaml.cs (C# page to create methods to that manipulate data)
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());
}
}
}
StudentList.xaml.cs (C# page to populate list view with database items)
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);
}
}
Aucun commentaire:
Enregistrer un commentaire