You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.0 KiB
160 lines
4.0 KiB
using MVVMDatabaseSample.Model;
|
|
using MVVMDatabaseSample.Util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows.Input;
|
|
|
|
namespace MVVMDatabaseSample.ViewModel
|
|
{
|
|
internal class ViewModelMain : INotifyPropertyChanged
|
|
{
|
|
private Student _student = new Student();
|
|
private ObservableCollection<Student> _sampleData;
|
|
private ICommand _connectCommand;
|
|
private ICommand _selectCommand;
|
|
private ICommand _loadedCommand;
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
public string Name
|
|
{
|
|
get { return _student.Name; }
|
|
set
|
|
{
|
|
_student.Name = value;
|
|
OnPropertyChanged("Name");
|
|
}
|
|
}
|
|
|
|
public int Age
|
|
{
|
|
get { return _student.Age; }
|
|
set
|
|
{
|
|
_student.Age = value;
|
|
OnPropertyChanged("Age");
|
|
}
|
|
}
|
|
|
|
public string Grage
|
|
{
|
|
get { return _student.Grade; }
|
|
set
|
|
{
|
|
_student.Grade = value;
|
|
OnPropertyChanged("Grade");
|
|
}
|
|
}
|
|
|
|
public int Score
|
|
{
|
|
get { return _student.Score; }
|
|
set
|
|
{
|
|
_student.Score = value;
|
|
OnPropertyChanged("Score");
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<Student> SampleData
|
|
{
|
|
get
|
|
{
|
|
if (_sampleData == null)
|
|
_sampleData = new ObservableCollection<Student>();
|
|
|
|
return _sampleData;
|
|
}
|
|
set { _sampleData = value; }
|
|
}
|
|
|
|
public ICommand ConnectCommand
|
|
{
|
|
get
|
|
{
|
|
if (_connectCommand == null)
|
|
_connectCommand = new DelegateCommand(Connect);
|
|
|
|
return _connectCommand;
|
|
}
|
|
}
|
|
|
|
public ICommand SelectCommand
|
|
{
|
|
get
|
|
{
|
|
if (_selectCommand == null)
|
|
_selectCommand = new DelegateCommand(Select);
|
|
|
|
return _selectCommand;
|
|
}
|
|
}
|
|
|
|
public ICommand LoadedCommand
|
|
{
|
|
get
|
|
{
|
|
if (_loadedCommand == null)
|
|
_loadedCommand = new DelegateCommand(Load);
|
|
|
|
return _loadedCommand;
|
|
}
|
|
}
|
|
|
|
private void Connect()
|
|
{
|
|
string message;
|
|
if (!SqlDBManager.Instance.GetConnection())
|
|
message = "Fail to connect";
|
|
else
|
|
message = "Success to connect";
|
|
|
|
MessageBox.Show(message);
|
|
}
|
|
|
|
private void Select()
|
|
{
|
|
DataSet ds = new DataSet();
|
|
string qry = "SELECT * FROM Student";
|
|
SqlDBManager.Instance.ExecuteQuery(ds, qry);
|
|
if (ds.Tables.Count < 1)
|
|
return;
|
|
|
|
foreach (DataRow row in ds.Tables[0].Rows)
|
|
{
|
|
Student student = new Student()
|
|
{
|
|
Name = row["Name"].ToString(),
|
|
Age = (int)row["Age"],
|
|
Grade = row["Grade"].ToString(),
|
|
Score = (int)row["Score"],
|
|
};
|
|
_sampleData.Add(student);
|
|
}
|
|
}
|
|
|
|
private void Load()
|
|
{
|
|
string message;
|
|
if (!SqlDBManager.Instance.GetConnection())
|
|
message = "Fail to connect";
|
|
else
|
|
message = "Success to connect";
|
|
|
|
MessageBox.Show(message);
|
|
}
|
|
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
}
|
|
|