initialize
This commit is contained in:
parent
83a260adfe
commit
61c232c216
@ -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<MainEditor, Students>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<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
|
||||
@ -27,35 +62,38 @@ namespace StudentManager.Common
|
||||
// update ** set ** = '**',... where ** = **;
|
||||
// insert into ** values('**','**'...);
|
||||
// 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);
|
||||
|
||||
|
||||
List<T> result = new List<T>();
|
||||
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
|
||||
{
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
23
StudentManager/Data/StudentData.cs
Normal file
23
StudentManager/Data/StudentData.cs
Normal 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(); }
|
||||
}
|
||||
}
|
@ -6,7 +6,30 @@
|
||||
xmlns:local="clr-namespace:StudentManager.Editor"
|
||||
mc:Ignorable="d"
|
||||
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>
|
||||
|
||||
<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>
|
||||
</Window>
|
||||
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// MainEditor.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class MainEditor : Window
|
||||
{
|
||||
public MainEditor()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
Loaded += MainEditor_Loaded;
|
||||
}
|
||||
|
||||
private void MainEditor_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SQLHelper.Query();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
34
StudentManager/Model/Students.cs
Normal file
34
StudentManager/Model/Students.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Model\" />
|
||||
<Folder Include="Interface\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user