From 2c793d21aff489c611b5c92fafb296719be426cb Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 19 Jul 2023 16:37:21 +0900 Subject: [PATCH] mvvm to mssql --- PacticeSolution/MSSQL_MVVM_Sample/App.xaml | 9 + PacticeSolution/MSSQL_MVVM_Sample/App.xaml.cs | 17 ++ .../MSSQL_MVVM_Sample/AssemblyInfo.cs | 10 + .../MSSQL_MVVM_Sample/Database/SqlManager.cs | 177 ++++++++++++++++++ .../MSSQL_MVVM_Sample.csproj | 14 ++ .../MSSQL_MVVM_Sample/MainWindow.xaml | 44 +++++ .../MSSQL_MVVM_Sample/MainWindow.xaml.cs | 28 +++ .../MSSQL_MVVM_Sample/Model/Student.cs | 19 ++ .../Structure/DelegateCommand.cs | 49 +++++ .../MSSQL_MVVM_Sample/Structure/Singleton.cs | 17 ++ .../ViewModel/MainViewModel.cs | 140 ++++++++++++++ PacticeSolution/PacticeSolution.sln | 8 +- 12 files changed, 531 insertions(+), 1 deletion(-) create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/App.xaml create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/App.xaml.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/AssemblyInfo.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/Database/SqlManager.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/MSSQL_MVVM_Sample.csproj create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/MainWindow.xaml create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/MainWindow.xaml.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/Model/Student.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/Structure/DelegateCommand.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/Structure/Singleton.cs create mode 100644 PacticeSolution/MSSQL_MVVM_Sample/ViewModel/MainViewModel.cs diff --git a/PacticeSolution/MSSQL_MVVM_Sample/App.xaml b/PacticeSolution/MSSQL_MVVM_Sample/App.xaml new file mode 100644 index 0000000..7364a49 --- /dev/null +++ b/PacticeSolution/MSSQL_MVVM_Sample/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/PacticeSolution/MSSQL_MVVM_Sample/App.xaml.cs b/PacticeSolution/MSSQL_MVVM_Sample/App.xaml.cs new file mode 100644 index 0000000..9a80667 --- /dev/null +++ b/PacticeSolution/MSSQL_MVVM_Sample/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace MSSQL_MVVM_Sample +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/PacticeSolution/MSSQL_MVVM_Sample/AssemblyInfo.cs b/PacticeSolution/MSSQL_MVVM_Sample/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/PacticeSolution/MSSQL_MVVM_Sample/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/PacticeSolution/MSSQL_MVVM_Sample/Database/SqlManager.cs b/PacticeSolution/MSSQL_MVVM_Sample/Database/SqlManager.cs new file mode 100644 index 0000000..6287f14 --- /dev/null +++ b/PacticeSolution/MSSQL_MVVM_Sample/Database/SqlManager.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MSSQL_MVVM_Sample.Structure; + +namespace MSSQL_MVVM_Sample.Database +{ + public sealed class SqlManager : Singleton + { + public event ExceptionEventHandler ExceptionEvent; + + public SqlConnection Connection { get; set; } + public string ConnectionString { get; set; } + + private SqlCommand _lastExecutedCommand; + private int _retryCount; + + public bool IsRunning { get { return CheckDBConnected(); } } + + public static SqlManager GetNewInstanceConnection() + { + if (Instance == null) + return null; + + SqlManager sqlManager = new SqlManager() + { + ConnectionString = Instance.ConnectionString + }; + sqlManager.GetConnection(); + sqlManager.ExceptionEvent = Instance.ExceptionEvent; + + return sqlManager; + } + + public void SetConnectionString(string connectionString) + { + ConnectionString = connectionString; + } + + public bool GetConnection() + { + try + { + if (Connection != null) + { + Connection.Close(); + Connection.Dispose(); + Connection = null; + } + + Connection = new SqlConnection(ConnectionString); + Connection.Open(); + + if (Connection.State != ConnectionState.Open) + return false; + + return true; + } + catch (Exception ex) + { + HandleExceptionEvent("GetConnection", ex); + return false; + } + } + + private bool CheckDBConnected() + { + try + { + string query = "SELECT GETDATE() Date"; + SqlCommand cmd = new SqlCommand() + { + Connection = Connection, + CommandText = query, + }; + + SqlDataReader result = cmd.ExecuteReader(); + if (result == null || !result.HasRows) + return false; + + return true; + } + catch (Exception ex) + { + HandleExceptionEvent("CheckDBConnected", ex); + return false; + } + } + + public int ExecuteNonQuery(string query) + { + try + { + lock (this) + { + SqlCommand cmd = new SqlCommand() + { + Connection = Connection, + CommandText = query, + }; + + return cmd.ExecuteNonQuery(); + } + } + catch (Exception ex) + { + HandleExceptionEvent("ExecuteNonQuery", ex); + return -1; + } + } + + public int ExecuteNonQuery(string query, SqlParameter[] sqlParams) + { + try + { + lock (this) + { + SqlCommand cmd = new SqlCommand() + { + Connection = Connection, + CommandText = query, + }; + + for (int i = 0; i < sqlParams.Length; i++) + { + cmd.Parameters.Add(sqlParams); + } + + return cmd.ExecuteNonQuery(); + } + } + catch (Exception ex) + { + HandleExceptionEvent("ExecuteNonQuery with sqlParams", ex); + return -1; + } + } + + public DataSet ExecuteSelectQuery(string query) + { + try + { + lock (this) + { + SqlCommand cmd = new SqlCommand() + { + Connection = Connection, + CommandText = query, + }; + SqlDataAdapter adt = new SqlDataAdapter() + { + SelectCommand = cmd, + }; + + DataSet ds = new DataSet(); + adt.Fill(ds); + return ds; + } + } + catch (Exception ex) + { + HandleExceptionEvent("ExecuteNonQuery with sqlParams", ex); + return null; + } + } + + private void HandleExceptionEvent(string id, Exception ex) + { + if (ExceptionEvent != null) + ExceptionEvent(id, ex); + } + } +} diff --git a/PacticeSolution/MSSQL_MVVM_Sample/MSSQL_MVVM_Sample.csproj b/PacticeSolution/MSSQL_MVVM_Sample/MSSQL_MVVM_Sample.csproj new file mode 100644 index 0000000..eda450c --- /dev/null +++ b/PacticeSolution/MSSQL_MVVM_Sample/MSSQL_MVVM_Sample.csproj @@ -0,0 +1,14 @@ + + + + WinExe + net6.0-windows + enable + true + + + + + + + diff --git a/PacticeSolution/MSSQL_MVVM_Sample/MainWindow.xaml b/PacticeSolution/MSSQL_MVVM_Sample/MainWindow.xaml new file mode 100644 index 0000000..676d121 --- /dev/null +++ b/PacticeSolution/MSSQL_MVVM_Sample/MainWindow.xaml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + +