如何在Xamarin中将特定数据更新到SQLite - c#

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

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

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

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

ViewController2:普通View Controller,具有3个文本字段和1个切换按钮,可获取所有4个用户详细信息。

ViewController3:一个按钮,使用户可以点击并打开出席(布尔值)。

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

要插入新的学生数据,代码应如下所示:
*(默认情况下,出勤率是“ 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中将“ Attendance”(布尔值)更新为true,则学生名为“ Apple”。我可以知道怎么做吗?

参考方案

使用下面的代码,在任何需要的地方,例如按钮内事件。但我建议您使用StudentId而不是StudentName进行此操作。

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

//update record afermodification   
UpdateContact(studentItem)

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

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

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

SQLite-无法连接数据库 - c#

背景:1.它是一个使用Sqlite databse的C#应用​​程序2.如果不存在db,则此应用程序将创建一个空的db,并将数据从服务器同步到该数据库。问题:1.有时应用程序突然崩溃。似乎有一些未处理的异常。2.即使我重新启动该应用程序,它也立即崩溃。临时解决方案:1.将数据库复制到其他位置,然后删除原始数据库。2.运行该应用程序。没有崩溃。由于不存在任何数…

Xamarin Forms按钮单击事件在运行时崩溃 - c#

我正在Xamarin中针对Windows,iOS和Android编辑一个应用程序。在可移植项目中,当编辑xaml文件时,我添加了以下按钮:<Button x:Name="Marker_Detection" Text="Marker Detection" Grid.Row="0" Grid.Co…

Xamarin.Forms目标框架(Kitkat)问题 - c#

我有一个Xamarin Forms中的项目,该项目以Target Framework作为Oreo即8.0开始。但是我的应用程序在低端设备上经常崩溃,因此我决定降级为Android 4.4作为目标框架。我将所有的nuget软件包降级为合适的版本。但是我仍然收到以下错误:max res 19, skipping values-v21 "max res …

SQLite PDO绑定不起作用? - php

我觉得我对此一无所知。我有三个简单的表。一个用户表,一个角色表和一个role_user表,该表以多对多关系连接用户和角色。对于用户角色,我有以下代码:$query = $pdo->prepare('select roles.* from roles inner join role_user on roles.id = role_user.ro…

SQLite.Net,相当于SQLite位数据类型的C# - c#

我在C#中声明以下课程[Table("Employee")] public class Employee { [PrimaryKey,AutoIncrement] public int EmployeeId { get; set; } public DateTime DateOfJoining { get; set; } public s…