This commit is contained in:
lwh 2023-05-31 21:29:30 +08:00
parent ecd2c34bc1
commit e7c74636f1
9 changed files with 339 additions and 34 deletions

View File

@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DMSkin.CyanDesign", "Design
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CyanDesignDemo", "Demos\CyanDesignDemo\CyanDesignDemo.csproj", "{F4C3340C-A5E8-4324-B512-AE8C10C00E14}"
EndProject
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Aerwen工具箱", "Aerwen工具箱\Aerwen工具箱.vdproj", "{C8F85820-377F-4E8D-9B2A-A8E5601E5030}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,8 @@ Global
{F4C3340C-A5E8-4324-B512-AE8C10C00E14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4C3340C-A5E8-4324-B512-AE8C10C00E14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4C3340C-A5E8-4324-B512-AE8C10C00E14}.Release|Any CPU.Build.0 = Release|Any CPU
{C8F85820-377F-4E8D-9B2A-A8E5601E5030}.Debug|Any CPU.ActiveCfg = Debug
{C8F85820-377F-4E8D-9B2A-A8E5601E5030}.Release|Any CPU.ActiveCfg = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -125,6 +125,9 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="DataBaseWindow.xaml.cs">
<DependentUpon>DataBaseWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Login.xaml.cs">
<DependentUpon>Login.xaml</DependentUpon>
</Compile>
@ -182,6 +185,10 @@
<Compile Include="View\Business\PageOpenFilesDialog.xaml.cs">
<DependentUpon>PageOpenFilesDialog.xaml</DependentUpon>
</Compile>
<Page Include="DataBaseWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Login.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -0,0 +1,110 @@
<Window
x:Class="CyanDesignDemo.DataBaseWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CyanDesignDemo"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="连接数据库"
Width="600"
Height="700"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Grid Margin="80,30">
<StackPanel>
<!-- 图标 -->
<StackPanel HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal">
<StackPanel>
<Image
Width="80"
Height="80"
Source="http://tool.aerwen.net/prod-api/Uploads/uploads/20230531/97B9720BC3D144A5.png" />
<TextBlock
HorizontalAlignment="Center"
FontSize="15"
Text="Aerwen" />
</StackPanel>
<StackPanel
Width="80"
Height="2"
Margin="10,0"
Background="#cccccc" />
<StackPanel>
<Image
Width="80"
Height="80"
Source="http://tool.aerwen.net/prod-api/Uploads/uploads/20230531/998B0E73FE12A9EE.png" />
<TextBlock
HorizontalAlignment="Center"
FontSize="15"
Text="数据库" />
</StackPanel>
</StackPanel>
</StackPanel>
<!-- 表单 -->
<StackPanel Margin="0,30,0,0">
<TextBox
x:Name="IPTextBox"
Margin="0,0,0,25"
VerticalAlignment="Center"
materialDesign:HintAssist.Hint="IP地址"
materialDesign:TextFieldAssist.HasClearButton="True"
FontSize="18"
Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
<TextBox
x:Name="DataBaseTextBox"
Margin="0,0,0,25"
VerticalAlignment="Center"
materialDesign:HintAssist.Hint="数据库名称"
materialDesign:TextFieldAssist.HasClearButton="True"
FontSize="18"
Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
<TextBox
x:Name="UserNameTextBox"
Margin="0,0,0,25"
VerticalAlignment="Center"
materialDesign:HintAssist.Hint="用户名"
materialDesign:TextFieldAssist.HasClearButton="True"
FontSize="18"
Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
<TextBox
x:Name="PassWordTextBox"
Margin="0,0,0,25"
VerticalAlignment="Center"
materialDesign:HintAssist.Hint="密码"
materialDesign:TextFieldAssist.HasClearButton="True"
FontSize="18"
Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
</StackPanel>
<StackPanel
Margin="0,50,0,0"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button Click="Button_Click" Content="保存" />
<Button
Margin="30,0,0,0"
Click="Button_Click_1"
Content="取消" />
</StackPanel>
</StackPanel>
</Grid>
</Window>

View File

@ -0,0 +1,193 @@
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
namespace CyanDesignDemo
{
/// <summary>
/// DataBaseWindow.xaml 的交互逻辑
/// </summary>
public partial class DataBaseWindow : Window
{
public DataBaseWindow()
{
InitializeComponent();
GetValue();
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
var connectionStringTemplate = "server=IP地址;Database=数据库名称;Uid=用户名;Pwd=密码;";
var ip = IPTextBox.Text;
var dataBase = DataBaseTextBox.Text;
var userName = UserNameTextBox.Text;
var passWord = PassWordTextBox.Text;
// 替换占位符
var connectionString = connectionStringTemplate
.Replace("IP地址", ip)
.Replace("数据库名称", dataBase)
.Replace("用户名", userName)
.Replace("密码", passWord);
// 写入文件
var filePath = "dataBase.txt";
if (File.Exists(filePath))
{
// 文件存在,执行打开文件的逻辑操作
File.WriteAllText(filePath, connectionString);
}
else
{
AddFile(filePath, "server=IP地址;Database=数据库名称;Uid=用户名;Pwd=密码;");
File.WriteAllText(filePath, connectionString);
}
string connectionStringText = File.ReadAllText("dataBase.txt");
try
{
using (MySqlConnection connection = new MySqlConnection(connectionStringText))
{
connection.Open();
MessageBox.Show("连接成功!");
this.Hide();
}
}
catch (Exception ex)
{
MessageBox.Show($"连接失败,请检查连接字符串: {ex.Message}");
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Hide();
}
// 打开文件的方法
private void OpenFile(string filePath)
{
try
{
Process.Start(filePath);
}
catch (Exception ex)
{
// 处理异常情况
MessageBox.Show($"无法打开文件: {ex.Message}");
}
}
// 添加文件的方法
private void AddFile(string filePath, string content)
{
try
{
// 检查文件是否已存在
if (File.Exists(filePath))
{
MessageBox.Show("文件已存在。");
return;
}
// 写入文件内容
File.WriteAllText(filePath, content);
// 文件添加成功后,执行打开文件操作
OpenFile(filePath);
}
catch (Exception ex)
{
// 处理异常情况
MessageBox.Show($"无法添加文件: {ex.Message}");
}
}
public void GetValue()
{
var filePath = "dataBase.txt";
if (File.Exists(filePath))
{
string connectionStringText = File.ReadAllText(filePath);
var connectionValues = connectionStringText.Split(';');
foreach (var value in connectionValues)
{
var keyValue = value.Split('=');
if (keyValue.Length == 2)
{
var key = keyValue[0].Trim().ToLower();
var val = keyValue[1].Trim();
switch (key)
{
case "server":
case "data source":
case "ip":
IPTextBox.Text = val;
break;
case "database":
DataBaseTextBox.Text = val;
break;
case "uid":
case "user":
case "username":
UserNameTextBox.Text = val;
break;
case "pwd":
case "password":
PassWordTextBox.Text = val;
break;
default:
// Handle unrecognized key or value
break;
}
}
}
}
}
}
}

View File

@ -85,20 +85,8 @@ namespace CyanDesignDemo
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string filePath = "dataBase.txt";
if (File.Exists(filePath))
{
// 文件存在,执行打开文件的逻辑操作
OpenFile(filePath);
}
else
{
// 文件不存在,执行添加文件的逻辑操作
AddFile(filePath, "server=127.0.0.1;Database=database;Uid=uid;Pwd=pwd;SslMode=none;CharSet=utf8mb4;AllowLoadLocalInfile=true;AllowUserVariables=true;");
// 添加文件后立即打开
OpenFile(filePath);
}
DataBaseWindow dataBaseWindow = new DataBaseWindow();
dataBaseWindow.Show();
}

View File

@ -467,7 +467,7 @@
<StackPanel Margin="40,0,0,0">
<!-- 数据库选择 -->
<TextBlock Margin="0,0,20,0" Text="默认选择Mysql" />
<TextBlock Margin="0,0,20,0" Text="暂时只支持 Mysql" />
<StackPanel Orientation="Horizontal">
<RadioButton
@ -493,14 +493,18 @@
IsThreeState="False" />
<StackPanel Margin="0,15,0,0" Orientation="Horizontal">
<Button
Margin="0,0,15,0"
Click="Button_Click_1"
Content="编辑" />
<Button
Margin="0,0,15,0"
Command="{Binding ConnectToDatabaseCommand}"
Content="连接" />
Content="测试连接" />
<Button
Margin="0,0,15,0"
Command="{Binding OpenConnectFilesCommand}"
Content="编辑" />
Content="手动编辑" />
</StackPanel>
</StackPanel>

View File

@ -91,6 +91,12 @@ namespace CyanDesignDemo.View.Business
createTableViewModel.GenField.FiledDecimal = mappingValue.FiledDecimal;
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
DataBaseWindow dataBaseWindowdataBaseWindow = new DataBaseWindow();
dataBaseWindowdataBaseWindow.Show();
}
}
}

View File

@ -52,7 +52,7 @@
<Button
Margin="0,0,20,0"
Command="{Binding ConnectToDatabaseCommand}"
Content="连接" />
Content="测试连接" />
<Button
Margin="0,0,20,0"
Command="{Binding OpenConnectFilesCommand}"

View File

@ -302,20 +302,8 @@ namespace DMSkinDemo.ViewModel.Business
/// </summary>
public ICommand OpenConnectFilesCommand => new DelegateCommand(obj =>
{
string filePath = "dataBase.txt";
if (File.Exists(filePath))
{
// 文件存在,执行打开文件的逻辑操作
OpenFile(filePath);
}
else
{
// 文件不存在,执行添加文件的逻辑操作
AddFile(filePath, "server=127.0.0.1;Database=database;Uid=uid;Pwd=pwd;SslMode=none;CharSet=utf8mb4;AllowLoadLocalInfile=true;AllowUserVariables=true;");
// 添加文件后立即打开
OpenFile(filePath);
}
DataBaseWindow dataBaseWindow = new DataBaseWindow();
dataBaseWindow.Show();
});
@ -376,7 +364,7 @@ namespace DMSkinDemo.ViewModel.Business
private void GetDataBaeTableList()
{
MindTableList = new ObservableCollection<MindTable>();
MindTableList.Clear();
try
{
string connectionString = File.ReadAllText("dataBase.txt");
@ -405,13 +393,18 @@ namespace DMSkinDemo.ViewModel.Business
foreach (var table in tables)
{
// 获取表的字段信息
DataTable columnsSchema = connection.GetSchema("Columns", new[] { null, null, table.MindTableName });
DataTable columnsSchema = connection.GetSchema("Columns", new[] { null, databaseName, table.MindTableName });
DataView columnsView = new DataView(columnsSchema);
columnsView.Sort = "ORDINAL_POSITION ASC";
foreach (DataRowView rowView in columnsView)
{
DataRow row = rowView.Row;
string columnName = row["COLUMN_NAME"].ToString();
// 判断当前字段名是否已存在于列表中,如果存在则跳过该次循环
if (table.MindTableFieldList.Any(field => field.MindTableFieldName == columnName))
{
continue;
}
string columnType = row["DATA_TYPE"].ToString();
string columnSize = (row["CHARACTER_MAXIMUM_LENGTH"] ?? 0).ToString();
string columnDecimalDigits = (row["NUMERIC_SCALE"] ?? 0).ToString();