【问题标题】:Update Collection Value with Database in UWP(MVVM)在 UWP(MVVM) 中使用数据库更新集合值
【发布时间】:2017-04-01 21:30:49
【问题描述】:

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

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

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

【问题讨论】:

  • 什么样的数据库?你具体是怎么用的?
  • mysql .. 首先我将学生数据添加到集合中...并将集合值添加到数据库中。如果我更新集合中的值,则数据库值也应该更新。我正在使用相同的集合来添加新值和检索现有值。

标签: c# mvvm uwp


【解决方案1】:

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

首先,您需要知道如何使用uwp 应用程序对MySQL 数据库进行CRUD 操作。为此,请参考this sample。

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

我根据你的描述写的数据层类,其中包含如何从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);
  }

对于完整的演示,您可以下载here。

【讨论】:

  • 感谢@Sunteen Wu
猜你喜欢
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
相关资源
最近更新 更多