|
|
|
|
using MSSQL_MVVM_Sample.Database;
|
|
|
|
|
using MSSQL_MVVM_Sample.Model;
|
|
|
|
|
using MSSQL_MVVM_Sample.Structure;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
|
|
|
|
namespace MSSQL_MVVM_Sample.ViewModel
|
|
|
|
|
{
|
|
|
|
|
internal class MainViewModel : INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
private Student _student;
|
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
|
|
|
|
|
|
public MainViewModel()
|
|
|
|
|
{
|
|
|
|
|
_student = new Student();
|
|
|
|
|
SqlManager.Instance.ConnectionString = "Data Source=peacecloud.synology.me,21433;Initial Catalog=Study;User ID=study;Password=Study1234";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return _student.Name; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_student.Name = value;
|
|
|
|
|
OnPropertyChanged(nameof(this.Name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Age
|
|
|
|
|
{
|
|
|
|
|
get { return _student.Age; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_student.Age = value;
|
|
|
|
|
OnPropertyChanged(nameof(this.Age));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Grade
|
|
|
|
|
{
|
|
|
|
|
get { return _student.Grade; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_student.Grade = value;
|
|
|
|
|
OnPropertyChanged(nameof(this.Grade));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Score
|
|
|
|
|
{
|
|
|
|
|
get { return _student.Score; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_student.Score = value;
|
|
|
|
|
OnPropertyChanged(nameof(this.Score));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ObservableCollection<Student> _sampleData;
|
|
|
|
|
public ObservableCollection<Student> SampleData
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_sampleData == null)
|
|
|
|
|
_sampleData = new ObservableCollection<Student>();
|
|
|
|
|
|
|
|
|
|
return _sampleData;
|
|
|
|
|
}
|
|
|
|
|
set { _sampleData = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ICommand _connectCommand;
|
|
|
|
|
public ICommand ConnectCommand
|
|
|
|
|
{
|
|
|
|
|
get { return _connectCommand ?? (_connectCommand = new DelegateCommand(ConnectDatabase)); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ICommand _searchCommand;
|
|
|
|
|
public ICommand SearchCommnad
|
|
|
|
|
{
|
|
|
|
|
get { return _searchCommand ?? (_searchCommand = new DelegateCommand(SearchData)); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConnectDatabase()
|
|
|
|
|
{
|
|
|
|
|
if (!SqlManager.Instance.GetConnection())
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Fail to connect to database", "Error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageBox.Show("Success to connect to database", "Inform");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchData()
|
|
|
|
|
{
|
|
|
|
|
if (this.SampleData != null)
|
|
|
|
|
this.SampleData.Clear();
|
|
|
|
|
|
|
|
|
|
string query = @"SELECT * FROM Student";
|
|
|
|
|
|
|
|
|
|
DataSet ds = SqlManager.Instance.ExecuteSelectQuery(query);
|
|
|
|
|
if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count < 1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("No data in table", "Alert");
|
|
|
|
|
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"]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.SampleData.Add(student);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|