2016-11-18 60 views
0

我有自定義集合,其中包含student_id,student_name, student_mark。並且在數據庫中也有與表具有相同列的表。設計表格有更新現有學生的一些控件。使用UWP中的數據庫更新集合值(MVVM)

暫時所有的更新操作都是使用該自定義集合完成的。假設我們在收集和數據庫中有100個學生數據。任何更新操作都應該反映在集合中。但是我的疑問是,在關閉應用程序之前,如何使用數據庫更新這些值?

但是,當我打開應用程序的集合應具有存儲在數據庫中的所有值。

+0

什麼樣的數據庫?你究竟如何使用它? –

+0

mysql ..首先我將學生數據添加到集合中...並將集合值添加到數據庫。如果我更新集合中的值,數據庫值也應該更新。我正在使用相同的集合來添加新值並檢索現有值。 – Manikandan

回答

0

但我的疑問是什麼我如何與數據庫

首先,你需要知道如何做MySQL數據庫,UWP應用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, [email protected]_mark Where student_id [email protected]_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 [email protected]_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

+0

謝謝@Sunteen Wu – Manikandan

+0

@Manikandan,如果我幫助你,你能否把答案標記爲已接受? –

+0

是的,好吧,確定.. – Manikandan