如何在 Xamarin 中将特定数据更新到 SQLite [英] How to update specific data into SQLite in Xamarin

查看:29
本文介绍了如何在 Xamarin 中将特定数据更新到 SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SQLite 创建了一个数据库,用于存储学生详细信息,包括

I have created a database using SQLite which store student details including

Student Name (String)
Student Password (String)
Student ID (String)
Student Attendance(Boolean)

在我的解决方案中,有 3 个视图控制器,如下所述:

In my solution, there are 3 view controller as describe below:

ViewController1:表格视图,显示学生列表.

ViewController1 : Table View, Display List of Students.

ViewController2:普通视图控制器,带有 3 个文本字段和 1 个切换按钮,可获取所有 4 个用户详细信息.

ViewController2 : Normal View Controller with 3 text field and 1 switch button to get all 4 user detail.

ViewController3 : 一个按钮,允许用户点击,并打开出勤(布尔值).

ViewController3 : A button that enable user to tap, and turn the attendance (boolean) to on.

我想使用 ViewController3 的按钮更新特定学生(例如 StudentName = Apple)的数据.

I would like to update a specific student (eg. StudentName = Apple) data using ViewController3's button.

要插入新的学生数据,代码应该是这样的:*(默认情况下,出勤为假")

To insert new student data, code should be like this: *(By default, the Attendance is "false")

void SaveButton_Clicked(object sender, EventArgs e)
{
    using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
    {
        connection.Insert(new Student() { StudentName = AddStuNameTxt.Text, StudentId = AddStuIdTxt.Text, StudentPassword = AddStuPassTxt.Text, StudentAttendence = false});
        new UIAlertView("Updated !", "Student successfully created", null, "OK", null).Show();
    }
}

但是如果我想在 ViewController3 中将出勤"(布尔值)更新为 true,其中学生名为Apple".我可以知道怎么做吗?

But if I want to update the "Attendance"(Boolean) to true in ViewController3 where student named is "Apple". May i know how to do that?

推荐答案

使用下面的代码,任何你需要的地方,比如按钮点击事件.但我建议您使用 StudentId 而不是 StudentName 来执行此操作.

Use code below, Where ever you need like inside button click event. But I would suggest you do this using StudentId instead of StudentName.

//get specific student record
var studentItem=GetSingleRecord("Apple")
studentItem.Attendance=true; 

//update record afermodification   
UpdateContact(studentItem)

上面使用的方法看起来像这样.

Above used method look like this.

public Student GetSingleRecord(string Name)
{
  return connection.Table<Student>().FirstOrDefault(i => i.StudentName == Name);
}

public void UpdateContact(Student obj)
{
    connection.Update(obj);
}

这篇关于如何在 Xamarin 中将特定数据更新到 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆