You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.9 KiB
158 lines
5.9 KiB
|
1 week ago
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
//using System.Data.SqlClient;
|
||
|
|
using Microsoft.Data.SqlClient;
|
||
|
|
using System.Text;
|
||
|
|
using System.Windows;
|
||
|
|
using System.Windows.Controls;
|
||
|
|
using System.Windows.Data;
|
||
|
|
using System.Windows.Documents;
|
||
|
|
using System.Windows.Input;
|
||
|
|
using System.Windows.Media;
|
||
|
|
using System.Windows.Media.Imaging;
|
||
|
|
using System.Windows.Navigation;
|
||
|
|
using System.Windows.Shapes;
|
||
|
|
|
||
|
|
namespace SunlightAggregationManager.View
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// SettingPage.xaml 的交互逻辑
|
||
|
|
/// </summary>
|
||
|
|
public partial class SettingPage : UserControl
|
||
|
|
{
|
||
|
|
//调用配置文件
|
||
|
|
private readonly UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "Configini.ini");
|
||
|
|
|
||
|
|
public SettingPage()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
|
||
|
|
string[] Language = { "en-US", "zh-CN", "zh-TW" };
|
||
|
|
string[] SQMOD = { "Windows Authentication", "SQL SERVER" };
|
||
|
|
comboBoxLanguage.ItemsSource = Language;
|
||
|
|
TEXT_SQMOD.ItemsSource = SQMOD;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
comboBoxLanguage.Text = Configini.IniReadvalue("SYS", "Language");
|
||
|
|
TEXT_SQLIP.Text = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
||
|
|
TEXT_SQLNAME.Text = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
||
|
|
TEXT_SQMOD.Text = Configini.IniReadvalue("SQL_SERVER", "SQL3");
|
||
|
|
TEXT_SQLUSER.Text = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
||
|
|
TEXT_SQLPASWORD.Text = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
||
|
|
}
|
||
|
|
catch (Exception) { }
|
||
|
|
}
|
||
|
|
|
||
|
|
private async void test_Click(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
// UI 更新:显示正在测试
|
||
|
|
textlog.Text = "TEST";
|
||
|
|
// 建议使用静态缓存的画笔,避免频繁创建对象导致内存抖动
|
||
|
|
textlog.Foreground = Brushes.Blue;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
// 构建连接字符串
|
||
|
|
var builder = new SqlConnectionStringBuilder
|
||
|
|
{
|
||
|
|
DataSource = TEXT_SQLIP.Text,
|
||
|
|
InitialCatalog = TEXT_SQLNAME.Text,
|
||
|
|
IntegratedSecurity = TEXT_SQMOD.Text == "Windows Authentication",
|
||
|
|
TrustServerCertificate = true
|
||
|
|
};
|
||
|
|
|
||
|
|
if (!builder.IntegratedSecurity)
|
||
|
|
{
|
||
|
|
builder.UserID = TEXT_SQLUSER.Text;
|
||
|
|
builder.Password = TEXT_SQLPASWORD.Text;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 异步连接数据库
|
||
|
|
await using var conn_SC = new SqlConnection(builder.ConnectionString);
|
||
|
|
|
||
|
|
// 使用 OpenAsync 代替 Open,避免卡死界面
|
||
|
|
await conn_SC.OpenAsync();
|
||
|
|
using (var cmd = new SqlCommand("SELECT 1", conn_SC))
|
||
|
|
{
|
||
|
|
cmd.ExecuteReader();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception)
|
||
|
|
{
|
||
|
|
// 失败处理
|
||
|
|
textlog.Text = "Link timeout";
|
||
|
|
textlog.Foreground = Brushes.Red;
|
||
|
|
return; // 直接返回,不执行后续代码
|
||
|
|
}
|
||
|
|
|
||
|
|
// 成功处理
|
||
|
|
textlog.Text = "Link succeed";
|
||
|
|
textlog.Foreground = Brushes.Green;
|
||
|
|
|
||
|
|
Console.WriteLine("IP = "+TEXT_SQLIP.Text);
|
||
|
|
Console.WriteLine("DataBase = " + TEXT_SQLNAME.Text);
|
||
|
|
Console.WriteLine("Link succeed");
|
||
|
|
|
||
|
|
// 弹出对话框本身会阻塞 UI 线程
|
||
|
|
MessageBoxResult SqlShow = System.Windows.MessageBox.Show(
|
||
|
|
"Whether the connection is successfully saved",
|
||
|
|
"SQL",
|
||
|
|
MessageBoxButton.YesNo,
|
||
|
|
MessageBoxImage.Information);
|
||
|
|
|
||
|
|
if (SqlShow == MessageBoxResult.Yes)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL1", TEXT_SQLIP.Text);
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL2", TEXT_SQLNAME.Text);
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL3", TEXT_SQMOD.SelectedValue?.ToString());
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL4", TEXT_SQLUSER.Text);
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL5", TEXT_SQLPASWORD.Text);
|
||
|
|
|
||
|
|
Console.WriteLine("Save database settings");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void save_Click(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("SYS", "Language", comboBoxLanguage.SelectedValue?.ToString());
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL1", TEXT_SQLIP.Text);
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL2", TEXT_SQLNAME.Text);
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL3", TEXT_SQMOD.SelectedValue?.ToString());
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL4", TEXT_SQLUSER.Text);
|
||
|
|
Configini.IniWritevalue("SQL_SERVER", "SQL5", TEXT_SQLPASWORD.Text);
|
||
|
|
|
||
|
|
Console.WriteLine("Save global settings");
|
||
|
|
}
|
||
|
|
|
||
|
|
//是否启用tcp
|
||
|
|
private void EnableTCP_Checked(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("NETWORK", "TCP", "1");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void EnableTCP_Unchecked(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("NETWORK", "TCP", "0");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void EnableHTTP_Checked(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("NETWORK", "HTTP", "1");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void EnableHTTP_Unchecked(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("NETWORK", "HTTP", "0");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void EnableTLS_Checked(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("NETWORK", "TLS", "1");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void EnableTLS_Unchecked(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
Configini.IniWritevalue("NETWORK", "TLS", "0");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|