From 61c232c216894e9f9ec3e6f8bf2cb5b11c99a7ae Mon Sep 17 00:00:00 2001 From: wangxiner55 <47072643+wangxiner55@users.noreply.github.com> Date: Wed, 4 Sep 2024 21:43:10 +0800 Subject: [PATCH] initialize --- StudentManager/App.xaml.cs | 3 +- StudentManager/Common/SQLHelper.cs | 66 +++++++++++++++++++----- StudentManager/Data/Student.cs | 13 ----- StudentManager/Data/StudentData.cs | 23 +++++++++ StudentManager/Editor/MainEditor.xaml | 25 ++++++++- StudentManager/Editor/MainEditor.xaml.cs | 11 +--- StudentManager/Model/Students.cs | 34 ++++++++++++ StudentManager/StudentManager.csproj | 1 - 8 files changed, 136 insertions(+), 40 deletions(-) delete mode 100644 StudentManager/Data/Student.cs create mode 100644 StudentManager/Data/StudentData.cs create mode 100644 StudentManager/Model/Students.cs diff --git a/StudentManager/App.xaml.cs b/StudentManager/App.xaml.cs index 8fc4e4b..8b3f8d5 100644 --- a/StudentManager/App.xaml.cs +++ b/StudentManager/App.xaml.cs @@ -1,4 +1,5 @@ using StudentManager.Editor; +using StudentManager.Model; using System.Configuration; using System.Data; using System.Windows; @@ -17,7 +18,7 @@ namespace StudentManager protected override void RegisterTypes(IContainerRegistry containerRegistry) { - + containerRegistry.RegisterForNavigation(); } } diff --git a/StudentManager/Common/SQLHelper.cs b/StudentManager/Common/SQLHelper.cs index 2870872..0e00431 100644 --- a/StudentManager/Common/SQLHelper.cs +++ b/StudentManager/Common/SQLHelper.cs @@ -1,5 +1,6 @@ using DryIoc.ImTools; using MySql.Data.MySqlClient; +using StudentManager.Common; using StudentManager.Data; using System; using System.Collections.Generic; @@ -12,10 +13,44 @@ namespace StudentManager.Common { public static class SQLMapping { - public static T Mapping() + public static void Mapping(MySqlDataReader reader) { - typeof(T).Attributes.ToString(); + typeof(T).GetProperties().ForEach(x => { Debug.WriteLine(x.Name.ToLower()); }); } + + public static void Mapping(T data, MySqlDataReader reader) + { + if (typeof(T) == typeof(StudentData)) + { + + (data as StudentData).UID = (int)reader[0]; + (data as StudentData).Name = reader[1].ToString(); + (data as StudentData).Gender = reader[2].ToString(); + (data as StudentData).Contact = reader[3].ToString(); + (data as StudentData).Address = reader[4].ToString(); + (data as StudentData).Grade = (int)reader[5]; + } + else + { + + } + + + + //for (int i = 0; i < reader.FieldCount; ++i) + //{ + // data?.GetType().GetProperties().ForEach(x => { x.SetValue(reader[i].GetType(), reader[i]); }); + //} + } + + + } + + + + struct userTable + { + } public static class SQLHelper @@ -27,35 +62,38 @@ namespace StudentManager.Common // update ** set ** = '**',... where ** = **; // insert into ** values('**','**'...); // delete from ** where ** = **; - public static T Query(ref T table, params string[] queryParams) where T : IDataCommon + public static List Query(string tableName, params string[] queryParams) where T : IDataCommon, new() { connection = new MySqlConnection(config); - + + List result = new List(); try { connection.Open(); - + string queryStr = ""; if (queryParams.Length > 0) - { - - } - string queryStr = $"select * from {table.TableName};"; + queryStr = $"select * from {tableName};"; + else + queryStr = $"select * from {tableName};"; cmd = new MySqlCommand(queryStr, connection); MySqlDataReader reader = cmd.ExecuteReader(); - + + + T row = new T(); + while (reader.Read()) { - for (int i = 0; i < reader.FieldCount; ++i) - { - Debug.WriteLine(reader[i].ToString()); - } + SQLMapping.Mapping(row, reader); + result.Add(row); } + return result; } catch (Exception ex) { Console.WriteLine(ex.Message); + return null; } finally { diff --git a/StudentManager/Data/Student.cs b/StudentManager/Data/Student.cs deleted file mode 100644 index 7632455..0000000 --- a/StudentManager/Data/Student.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace StudentManager.Data -{ - public class Student - { - public int UId { get; set; } - } -} diff --git a/StudentManager/Data/StudentData.cs b/StudentManager/Data/StudentData.cs new file mode 100644 index 0000000..5da109b --- /dev/null +++ b/StudentManager/Data/StudentData.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentManager.Data +{ + public class StudentData : IDataCommon + { + public int UID { get; set; } = 1; + public string Name { get; set; } = "Name"; + public string Gender { get; set; } = "Gender"; + public string Contact { get; set; } = "Contact"; + public string Address { get; set; } = "Address"; + public int Grade { get; set; } = 1; + + + + + public string TableName { get => "users"; set => throw new NotImplementedException(); } + } +} diff --git a/StudentManager/Editor/MainEditor.xaml b/StudentManager/Editor/MainEditor.xaml index fff6ee7..7618555 100644 --- a/StudentManager/Editor/MainEditor.xaml +++ b/StudentManager/Editor/MainEditor.xaml @@ -6,7 +6,30 @@ xmlns:local="clr-namespace:StudentManager.Editor" mc:Ignorable="d" Title="MainEditor" Height="450" Width="800"> + + + - + + + + + + + + + + + + + + + diff --git a/StudentManager/Editor/MainEditor.xaml.cs b/StudentManager/Editor/MainEditor.xaml.cs index cb82ef4..8dd35ae 100644 --- a/StudentManager/Editor/MainEditor.xaml.cs +++ b/StudentManager/Editor/MainEditor.xaml.cs @@ -1,4 +1,5 @@ using StudentManager.Common; +using StudentManager.Data; using System; using System.Collections.Generic; using System.Linq; @@ -15,21 +16,11 @@ using System.Windows.Shapes; namespace StudentManager.Editor { - /// - /// MainEditor.xaml 的交互逻辑 - /// public partial class MainEditor : Window { public MainEditor() { InitializeComponent(); - - Loaded += MainEditor_Loaded; - } - - private void MainEditor_Loaded(object sender, RoutedEventArgs e) - { - SQLHelper.Query(); } } } diff --git a/StudentManager/Model/Students.cs b/StudentManager/Model/Students.cs new file mode 100644 index 0000000..91f6804 --- /dev/null +++ b/StudentManager/Model/Students.cs @@ -0,0 +1,34 @@ +using StudentManager.Common; +using StudentManager.Data; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentManager.Model +{ + public class Students + { + + private ObservableCollection studentDatas = new ObservableCollection(); + public ReadOnlyObservableCollection StudentDatas { get; private set; } + + + Students() + { + StudentDatas = new ReadOnlyObservableCollection(studentDatas); + + Query(); + } + + + + + private void Query() + { + SQLHelper.Query("users").ForEach(x => studentDatas.Add(x)); + } + } +} diff --git a/StudentManager/StudentManager.csproj b/StudentManager/StudentManager.csproj index fdcb385..cbd6c43 100644 --- a/StudentManager/StudentManager.csproj +++ b/StudentManager/StudentManager.csproj @@ -14,7 +14,6 @@ -