From 1ebe31a26c65214798e888dc273379ecc186900a Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 6 Jul 2025 11:09:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99=E8=AE=BE=E7=BD=AE=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 19 +++-- Config/ConnectionSettings.cs | 40 ++++++++++ Data/DbContext.cs | 7 +- PMSWPF.csproj | 2 + ViewModels/SettingViewModel.cs | 123 ++++++++++++++++++++++++++++++- Views/SettingView.xaml | 130 ++++++++++++++++++++++++++++++++- Views/SettingView.xaml.cs | 2 + 7 files changed, 312 insertions(+), 11 deletions(-) create mode 100644 Config/ConnectionSettings.cs diff --git a/App.xaml.cs b/App.xaml.cs index 0fc4f2c..a621e74 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -48,11 +48,20 @@ public partial class App : Application { base.OnStartup(e); await Host.StartAsync(); - InitializeDataBase(); - InitializeMenu() - .Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); }, - () => { MessageHelper.SendLoadMessage(LoadTypes.Menu); }); - Host.Services.GetRequiredService(); + + try + { + InitializeDataBase(); + InitializeMenu() + .Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); }, + () => { MessageHelper.SendLoadMessage(LoadTypes.Menu); }); + Host.Services.GetRequiredService(); + } + catch (Exception exception) + { + NotificationHelper.ShowMessage("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}",NotificationType.Error); + } + MainWindow = Host.Services.GetRequiredService(); MainWindow.Show(); } diff --git a/Config/ConnectionSettings.cs b/Config/ConnectionSettings.cs new file mode 100644 index 0000000..feecc4f --- /dev/null +++ b/Config/ConnectionSettings.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using Newtonsoft.Json; + +namespace PMSWPF.Config +{ + public class ConnectionSettings + { + public string DbType { get; set; } = "MySql"; + public string Server { get; set; } = "127.0.0.1"; + public int Port { get; set; } = 3306; + public string UserId { get; set; } = "root"; + public string Password { get; set; } = "Pgw15221236646"; + public string Database { get; set; } = "pmswpf"; + + private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "connectionSettings.json"); + + public static ConnectionSettings Load() + { + if (File.Exists(SettingsFilePath)) + { + string json = File.ReadAllText(SettingsFilePath); + return JsonConvert.DeserializeObject(json); + } + return new ConnectionSettings(); // Return default settings if file doesn't exist + } + + public void Save() + { + string json = JsonConvert.SerializeObject(this, Formatting.Indented); + File.WriteAllText(SettingsFilePath, json); + } + + public string ToConnectionString() + { + // This example is for MySQL. You'll need to adjust for other database types. + return $"server={Server};port={Port};user={UserId};password={Password};database={Database};"; + } + } +} diff --git a/Data/DbContext.cs b/Data/DbContext.cs index bdf66e2..731a849 100644 --- a/Data/DbContext.cs +++ b/Data/DbContext.cs @@ -6,11 +6,14 @@ public class DbContext { public static SqlSugarClient GetInstance() { - var connectionString = "server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=pmswpf;"; + var settings = PMSWPF.Config.ConnectionSettings.Load(); + var connectionString = settings.ToConnectionString(); + var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), settings.DbType); + var _db = new SqlSugarClient(new ConnectionConfig { ConnectionString = connectionString, - DbType = DbType.MySql, // 根据实际数据库类型修改,如DbType.MySql等 + DbType = dbType, // 根据实际数据库类型修改,如DbType.MySql等 IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); diff --git a/PMSWPF.csproj b/PMSWPF.csproj index f8808e7..3e9f55c 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -21,10 +21,12 @@ + + diff --git a/ViewModels/SettingViewModel.cs b/ViewModels/SettingViewModel.cs index f8123fa..997d042 100644 --- a/ViewModels/SettingViewModel.cs +++ b/ViewModels/SettingViewModel.cs @@ -1,6 +1,125 @@ +using CommunityToolkit.Mvvm.Input; +using PMSWPF.Config; +using PMSWPF.Data; +using PMSWPF.Helper; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + namespace PMSWPF.ViewModels; -public class SettingViewModel : ViewModelBase +public partial class SettingViewModel : ViewModelBase { - + private ConnectionSettings _connectionSettings; + + public SettingViewModel() + { + _connectionSettings = ConnectionSettings.Load(); + AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList(); + } + + public List AvailableDbTypes { get; set; } + + public string SelectedDbType + { + get => _connectionSettings.DbType; + set + { + if (_connectionSettings.DbType != value) + { + _connectionSettings.DbType = value; + OnPropertyChanged(); + _connectionSettings.Save(); + } + } + } + + public string Server + { + get => _connectionSettings.Server; + set + { + if (_connectionSettings.Server != value) + { + _connectionSettings.Server = value; + OnPropertyChanged(); + _connectionSettings.Save(); + } + } + } + + public int Port + { + get => _connectionSettings.Port; + set + { + if (_connectionSettings.Port != value) + { + _connectionSettings.Port = value; + OnPropertyChanged(); + _connectionSettings.Save(); + } + } + } + + public string UserId + { + get => _connectionSettings.UserId; + set + { + if (_connectionSettings.UserId != value) + { + _connectionSettings.UserId = value; + OnPropertyChanged(); + _connectionSettings.Save(); + } + } + } + + public string Password + { + get => _connectionSettings.Password; + set + { + if (_connectionSettings.Password != value) + { + _connectionSettings.Password = value; + OnPropertyChanged(); + _connectionSettings.Save(); + } + } + } + + public string Database + { + get => _connectionSettings.Database; + set + { + if (_connectionSettings.Database != value) + { + _connectionSettings.Database = value; + OnPropertyChanged(); + _connectionSettings.Save(); + } + } + } + + [RelayCommand] + private async Task TestConnection() + { + try + { + using (var db = DbContext.GetInstance()) + { + await db.Ado.OpenAsync(); + NotificationHelper.ShowMessage("连接成功!"); + } + } + catch (Exception ex) + { + NotificationHelper.ShowMessage($"连接失败:{ex.Message}"); + } + } } \ No newline at end of file diff --git a/Views/SettingView.xaml b/Views/SettingView.xaml index b6a8c67..a24e621 100644 --- a/Views/SettingView.xaml +++ b/Views/SettingView.xaml @@ -3,7 +3,133 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" + xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" mc:Ignorable="d" - d:DesignHeight="300" d:DesignWidth="300"> - + d:DesignHeight="450" + d:DesignWidth="800"> + + + + + + + +