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

时间:2016-11-18 05:48:55

标签: c# mvvm uwp

我有自定义集合,其中包含student_idstudent_namestudent_mark。并且表中的表也包含相同的列。设计表格有一些控制来更新现有学生。

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

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

1 个答案:

答案 0 :(得分:1)

  

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

首先,您需要知道如何使用uwp app对CRUD数据库执行MySQL操作。为此,请参考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