但我的疑问是如何使用数据库更新这些值
首先,您需要知道如何使用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。