initialize

This commit is contained in:
wangxiner55 2024-09-04 21:43:10 +08:00
parent 83a260adfe
commit 61c232c216
8 changed files with 136 additions and 40 deletions

View File

@ -1,4 +1,5 @@
using StudentManager.Editor; using StudentManager.Editor;
using StudentManager.Model;
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;
using System.Windows; using System.Windows;
@ -17,7 +18,7 @@ namespace StudentManager
protected override void RegisterTypes(IContainerRegistry containerRegistry) protected override void RegisterTypes(IContainerRegistry containerRegistry)
{ {
containerRegistry.RegisterForNavigation<MainEditor, Students>();
} }
} }

View File

@ -1,5 +1,6 @@
using DryIoc.ImTools; using DryIoc.ImTools;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using StudentManager.Common;
using StudentManager.Data; using StudentManager.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -12,10 +13,44 @@ namespace StudentManager.Common
{ {
public static class SQLMapping public static class SQLMapping
{ {
public static T Mapping<T>() public static void Mapping<T>(MySqlDataReader reader)
{ {
typeof(T).Attributes.ToString(); typeof(T).GetProperties().ForEach(x => { Debug.WriteLine(x.Name.ToLower()); });
} }
public static void Mapping<T>(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 public static class SQLHelper
@ -27,35 +62,38 @@ namespace StudentManager.Common
// update ** set ** = '**',... where ** = **; // update ** set ** = '**',... where ** = **;
// insert into ** values('**','**'...); // insert into ** values('**','**'...);
// delete from ** where ** = **; // delete from ** where ** = **;
public static T Query<T>(ref T table, params string[] queryParams) where T : IDataCommon public static List<T> Query<T>(string tableName, params string[] queryParams) where T : IDataCommon, new()
{ {
connection = new MySqlConnection(config); connection = new MySqlConnection(config);
List<T> result = new List<T>();
try try
{ {
connection.Open(); connection.Open();
string queryStr = "";
if (queryParams.Length > 0) if (queryParams.Length > 0)
{ queryStr = $"select * from {tableName};";
else
} queryStr = $"select * from {tableName};";
string queryStr = $"select * from {table.TableName};";
cmd = new MySqlCommand(queryStr, connection); cmd = new MySqlCommand(queryStr, connection);
MySqlDataReader reader = cmd.ExecuteReader(); MySqlDataReader reader = cmd.ExecuteReader();
T row = new T();
while (reader.Read()) while (reader.Read())
{ {
for (int i = 0; i < reader.FieldCount; ++i) SQLMapping.Mapping(row, reader);
{ result.Add(row);
Debug.WriteLine(reader[i].ToString());
}
} }
return result;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
return null;
} }
finally finally
{ {

View File

@ -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; }
}
}

View File

@ -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(); }
}
}

View File

@ -6,7 +6,30 @@
xmlns:local="clr-namespace:StudentManager.Editor" xmlns:local="clr-namespace:StudentManager.Editor"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainEditor" Height="450" Width="800"> Title="MainEditor" Height="450" Width="800">
<Window.Resources>
<Style x:Key="TextStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,5"/>
</Style>
</Window.Resources>
<Grid> <Grid>
<ListBox ItemsSource="{Binding StudentDatas}">
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction x:Name="menuTrigger" Command="{Binding navigateCommand}" CommandParameter="{Binding ElementName=menu, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>-->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding UID}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Name}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Gender}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Contact}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Address}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Grade}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,4 +1,5 @@
using StudentManager.Common; using StudentManager.Common;
using StudentManager.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -15,21 +16,11 @@ using System.Windows.Shapes;
namespace StudentManager.Editor namespace StudentManager.Editor
{ {
/// <summary>
/// MainEditor.xaml 的交互逻辑
/// </summary>
public partial class MainEditor : Window public partial class MainEditor : Window
{ {
public MainEditor() public MainEditor()
{ {
InitializeComponent(); InitializeComponent();
Loaded += MainEditor_Loaded;
}
private void MainEditor_Loaded(object sender, RoutedEventArgs e)
{
SQLHelper.Query();
} }
} }
} }

View File

@ -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<StudentData> studentDatas = new ObservableCollection<StudentData>();
public ReadOnlyObservableCollection<StudentData> StudentDatas { get; private set; }
Students()
{
StudentDatas = new ReadOnlyObservableCollection<StudentData>(studentDatas);
Query();
}
private void Query()
{
SQLHelper.Query<StudentData>("users").ForEach(x => studentDatas.Add(x));
}
}
}

View File

@ -14,7 +14,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Model\" />
<Folder Include="Interface\" /> <Folder Include="Interface\" />
</ItemGroup> </ItemGroup>