在UWP(MVVM)中使用数据库更新集合值

本文关键字:数据库 更新 集合 UWP MVVM | 更新日期: 2023-09-27 18:18:27

我有包含student_id, student_name, student_mark的自定义集合,并且在数据库中具有相同列的表。设计表单有一些用于更新现有学生的控件。

暂时所有的更新操作都是用自定义集合完成的。让我们假设在集合和数据库中有100个学生数据。任何更新操作都应反映在集合中。但是我的疑问是我如何在关闭应用程序之前用数据库更新这些值??

但是当我打开应用程序时,集合应该具有存储在数据库中的所有值。

在UWP(MVVM)中使用数据库更新集合值

但是我的疑问是我如何更新这些值与数据库

首先,您需要知道如何使用uwp应用程序在MySQL数据库上进行CRUD操作。为此,请参考本示例。

其次,根据您的描述,您已经构建了一个MVVM项目来将集合数据绑定到视图。但是你没有这个MVVM结构的数据层。为此,您需要为数据层创建一个类来执行GRUD操作,并从ViewModel与该数据服务建立联系。更多细节请参考本文。

根据您的描述,我编写的数据层类包含如何读取,更新和删除mysql数据库中的数据如下:

public class Student
{
    public int Student_id { get; set; }
    public string Student_name { get; set; }
    public string Student_mark { get; set; }
}
public class DataService
{
    static string connectionString;
    public static String Name = "Data Service.";
    private static ObservableCollection<Student> _allStudents = new ObservableCollection<Student>();
    public static ObservableCollection<Student> GetStudents()
    {
        try
        {
            string server = "127.0.0.1";
            string database = "sakila";
            string user = "root";
            string pswd = "!QAZ2wsx";
            connectionString = "Server = " + server + ";database = " + database + ";uid = " + user + ";password = " + pswd + ";SslMode=None;";
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                MySqlCommand getCommand = connection.CreateCommand();
                getCommand.CommandText = "SELECT * FROM student";
                using (MySqlDataReader reader = getCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        _allStudents.Add(new Student() { Student_id = reader.GetInt32(0), Student_name = reader.GetString(1), Student_mark = reader.GetString(2) });
                    }
                }
            }
        }
        catch (MySqlException sqlex)
        {
            // Handle it :)
        }
        return _allStudents;
    }
    public static bool InsertNewStudent(Student newStudent)
    {
        // Insert to the collection and update DB
        try
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                MySqlCommand insertCommand = connection.CreateCommand();
                insertCommand.CommandText = "INSERT INTO student(student_id, student_name, student_mark)VALUES(@student_id, @student_name,@student_mark)";
                insertCommand.Parameters.AddWithValue("@student_id", newStudent.Student_id);
                insertCommand.Parameters.AddWithValue("@student_name", newStudent.Student_name);
                insertCommand.Parameters.AddWithValue("@student_mark", newStudent.Student_mark);
                insertCommand.ExecuteNonQuery();                   
                return true;
            }
        }
        catch (MySqlException sqlex)
        {
            return false;
        }
    }
    public static bool UpdateStudent(Student Student)
    { 
        try
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                MySqlCommand insertCommand = connection.CreateCommand();
                insertCommand.CommandText = "Update student Set student_name= @student_name, student_mark=@student_mark Where student_id =@student_id";
                insertCommand.Parameters.AddWithValue("@student_id", Student.Student_id);
                insertCommand.Parameters.AddWithValue("@student_name", Student.Student_name);
                insertCommand.Parameters.AddWithValue("@student_mark", Student.Student_mark);
                insertCommand.ExecuteNonQuery();
                return true;
            }
        }
        catch (MySqlException sqlex)
        {
            // Don't forget to handle it
            return false;
        }
    }
    public static bool Delete(Student Student)
    {
        try
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                MySqlCommand insertCommand = connection.CreateCommand();
                insertCommand.CommandText = "Delete from sakila.student where student_id =@student_id";
                insertCommand.Parameters.AddWithValue("@student_id", Student.Student_id);
                insertCommand.ExecuteNonQuery();
                return true;
            }
        }
        catch (MySqlException sqlex)
        {                
            return false;
        }
    } 
}

对于以TwoWay绑定方式更新数据库,我们可以通过调用中的数据更新方法来实现PropertyChanged事件如下:

  void Person_OnNotifyPropertyChanged(Object sender, PropertyChangedEventArgs e)
  {
      organization.Update((StudentViewModel)sender);
  }

完整的演示可以在这里下载