基於WPF重複造輪子,寫一款資料庫檔案管理工具(一)

2022-07-31 18:00:45

專案背景

公司業務歷史悠久且複雜,資料庫的表更是多而繁雜,每次基於老業務做功能開發都需要去翻以前的表和業務程式碼。需要理解舊的表的用途以及包含的欄位的含義,表少還好說,但是表一多這就很浪費時間,而且留下來的檔案都是殘缺不全,每次查一些表的含義都要捯飭很久。在網上搜尋關於資料庫檔案管理工具搜到最多的就是Screw和DBCHM,一個是基於Java的工具、另一個則是bug很多,表一多就一直轉圈圈進不去。所以自己就動手開發了這款SmartSQL的工具。

它是一款基於.Net 4.6.1WPF開發的一款資料庫檔案管理,不僅支援多種資料庫(SQLServerMySQLPostgreSQLSQLite)表、檢視、儲存過程的查詢管理,還支援對其進行匯出成離線檔案,支援的檔案包括CHMWordExcelPDFHTMLXmlJsonMarkDown等多種格式。

現在將它開源分享出來,供更多的小夥伴使用和參考學習(文末附開源地址)。

技術棧

  • .Net 4.6.1
  • WPF
  • HandyControl
  • SqlSugar
  • AvalonEdit
  • SharpVectors

HandyControl是一款非常優秀的WPF框架,做出來的頁面都很漂亮,所以我們選擇使用它。
Nuget中參照HandyControl

一.選單欄

然後我們要實現一個基於WPF邊框上的選單欄,剛好HandyControl中有這麼一個選單欄的控制元件,
下面就是實現選單欄的方法:
`

<hc:GlowWindow.NonClientAreaContent>
    <StackPanel Height="29" Margin="25,0,0,0">
        <Menu HorizontalAlignment="Left">
            <MenuItem
                x:Name="SwitchMenu"
                Cursor="Hand"
                FontWeight="Bold"
                Foreground="{DynamicResource DarkPrimaryBrush}"
                Header="選擇連線">
                <MenuItem.Icon>
                    <Path
                        Data="{StaticResource DownGeometry}"
                        Fill="{DynamicResource DarkPrimaryBrush}"
                        Stretch="Uniform" />
                </MenuItem.Icon>
                <MenuItem.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <MenuItem
                            Width="160"
                            Margin="0"
                            Padding="0"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Stretch"
                            Click="SwitchMenu_Click"
                            Cursor="Hand"
                            FontWeight="Normal"
                            Header="{Binding ConnectName}">
                            <MenuItem.Icon>
                                <svgc:SvgViewbox
                                    Width="16"
                                    Height="16"
                                    HorizontalAlignment="Left"
                                    IsHitTestVisible="False"
                                    Source="{Binding Icon}" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </HierarchicalDataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>
            <MenuItem
                Name="MenuConnect"
                Cursor="Hand"
                FontWeight="Bold"
                Foreground="{DynamicResource DarkPrimaryBrush}"
                Header="檔案">
                <MenuItem.Icon>
                    <Path
                        Data="{StaticResource FileGeometry}"
                        Fill="{DynamicResource DarkPrimaryBrush}"
                        Stretch="Uniform" />
                </MenuItem.Icon>
                <MenuItem
                    Name="AddConnect"
                    Click="AddConnect_OnClick"
                    FontWeight="Normal"
                    Header="新建連線">
                    <MenuItem.Icon>
                        <Path
                            Data="{StaticResource NewConnectGeometry}"
                            Fill="{DynamicResource DarkPrimaryBrush}"
                            Stretch="Uniform" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem
                    Name="ImportMark"
                    Click="ImportMark_OnClick"
                    FontWeight="Normal"
                    Header="匯入備註">
                    <MenuItem.Icon>
                        <Path
                            Data="{StaticResource ImportGeometry}"
                            Fill="{DynamicResource DarkPrimaryBrush}"
                            Stretch="Uniform" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem
                    Name="ExportDoc"
                    Click="ExportDoc_OnClick"
                    FontWeight="Normal"
                    Header="匯出檔案">
                    <MenuItem.Icon>
                        <Path
                            Data="{StaticResource ExportGeometry}"
                            Fill="{DynamicResource DarkPrimaryBrush}"
                            Stretch="Uniform" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem
                Name="MenuGroup"
                Click="MenuGroup_OnClick"
                Cursor="Hand"
                FontWeight="Bold"
                Foreground="{DynamicResource DarkPrimaryBrush}"
                Header="分組">
                <MenuItem.Icon>
                    <Path
                        Data="{StaticResource GroupGeometry}"
                        Fill="{DynamicResource DarkPrimaryBrush}"
                        Stretch="Uniform" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem
                Name="MenuSetting"
                Click="MenuSetting_OnClick"
                Cursor="Hand"
                FontWeight="Bold"
                Foreground="{DynamicResource DarkPrimaryBrush}"
                Header="設定">
                <MenuItem.Icon>
                    <Path
                        Data="{StaticResource SettingGeometry}"
                        Fill="{DynamicResource DarkPrimaryBrush}"
                        Stretch="Uniform" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem
                Name="MenuAbout"
                Click="MenuAbout_OnClick"
                Cursor="Hand"
                FontWeight="Bold"
                Foreground="{DynamicResource DarkPrimaryBrush}"
                Header="關於">
                <MenuItem.Icon>
                    <Path
                        Data="{StaticResource InfoGeometry}"
                        Fill="{DynamicResource DarkPrimaryBrush}"
                        Stretch="Uniform" />
                </MenuItem.Icon>
            </MenuItem>
        </Menu>
    </StackPanel>
</hc:GlowWindow.NonClientAreaContent>
<!--  工具列選單  -->

其中有個小插曲,在WPF中是預設不支援svg圖形的,所以我們需要參照一個元件:SharpVectors,它的使用方法是這樣的,參照svg介面需要引入下面語句:
xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
然後參照要顯示的svg圖形:

<svgc:SvgViewbox
          Width="16"
          Height="16"
          HorizontalAlignment="Left"
          IsHitTestVisible="False"
          Source="{Binding Icon}" />

二.左側選單欄

然後就是左側的選單欄,我們要實現一個資料庫的選擇和資料庫物件的搜尋,可以搜尋相關表、檢視、儲存過程等物件。
首先我們要對我們的主介面進行一個簡單的1:1:1的豎向佈局,分別為左側選單欄、中間可以移動的分隔欄、右面的主介面:


    <!--  Main區域  -->
    <Grid x:Name="GridMain" Background="{StaticResource CloudDrawingBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3.3*" MinWidth="200" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="6.6*" />
        </Grid.ColumnDefinitions>
</Grid>

現在我們要實現一個左側樹形的選單欄,我們使用的是WPF裡面的TreeView控制元件進行實現這樣一個功能,下面是相關程式碼:


        <DockPanel Grid.Row="0" Grid.Column="0">
            <hc:SimplePanel>
                <Border
                    Margin="5,5,0,5"
                    Background="{DynamicResource RegionBrush}"
                    CornerRadius="{Binding CornerRadius}">
                    <Grid
                        Height="Auto"
                        Margin="5"
                        Background="Transparent">
                        <TextBox x:Name="HidSelectDatabase" Visibility="Hidden" />
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="8*" />
                                <ColumnDefinition Width="1*" MinWidth="30" />
                            </Grid.ColumnDefinitions>
                            <ComboBox
                                x:Name="SelectDatabase"
                                Height="30"
                                VerticalAlignment="Top"
                                HorizontalContentAlignment="Stretch"
                                hc:BorderElement.CornerRadius="5"
                                hc:InfoElement.Placeholder="請選擇資料庫"
                                Cursor="Hand"
                                IsTextSearchEnabled="True"
                                SelectionChanged="SelectDatabase_OnSelectionChanged"
                                Style="{StaticResource ComboBoxExtend}"
                                Text="{Binding DbName}">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
                                            <Image
                                                Width="11"
                                                Height="15"
                                                Source="/SmartSQL;component/Resources/Img/dataBase.ico" />
                                            <TextBlock
                                                Margin="5,0,0,0"
                                                HorizontalAlignment="Center"
                                                VerticalAlignment="Center"
                                                Text="{Binding DbName}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                            <Button
                                Name="BtnFresh"
                                Grid.Column="2"
                                Margin="0,0,0,0"
                                Padding="4"
                                VerticalAlignment="Top"
                                Background="Transparent"
                                BorderThickness="0"
                                Click="BtnFresh_OnClick"
                                Cursor="Hand">
                                <Button.Content>
                                    <Image Source="/SmartSQL;component/Resources/Img/Refresh.png" Stretch="Fill" />
                                </Button.Content>
                            </Button>
                        </Grid>
                        <hc:SearchBar
                            x:Name="SearchMenu"
                            Height="30"
                            Margin="0,34,0,0"
                            Padding="5,0,5,0"
                            VerticalAlignment="Top"
                            HorizontalContentAlignment="Stretch"
                            hc:BorderElement.CornerRadius="5"
                            hc:InfoElement.Placeholder="搜尋資料表/檢視/儲存過程"
                            FontSize="13"
                            ShowClearButton="True"
                            Style="{StaticResource SearchBarPlus}"
                            TextChanged="SearchMenu_OnTextChanged" />
                        <TabControl
                            x:Name="TabLeftType"
                            Margin="0,65,0,40"
                            SelectionChanged="TabLeftType_OnSelectionChanged"
                            Style="{StaticResource TabControlInLine}">
                            <TabItem
                                x:Name="TabAllData"
                                Cursor="Hand"
                                Header="全部"
                                IsSelected="True" />
                            <TabItem
                                x:Name="TabGroupData"
                                Cursor="Hand"
                                Header="分組"
                                IsSelected="False" />
                            <!--<TabItem
                                x:Name="TabFavData"
                                Cursor="Hand"
                                Header="收藏"
                                IsSelected="False" />-->
                        </TabControl>
                        <TreeView
                            x:Name="TreeViewTables"
                            Margin="0,100,0,0"
                            VerticalAlignment="Top"
                            BorderThickness="0"
                            ItemsSource="{Binding TreeViewData}"
                            SelectedItemChanged="SelectedTable_OnClick">
                            <TreeView.ItemContainerStyle>
                                <Style BasedOn="{StaticResource TreeViewItemBaseStyle}" TargetType="{x:Type TreeViewItem}">
                                    <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
                                    <Setter Property="FontWeight" Value="{Binding FontWeight}" />
                                    <Setter Property="FontSize" Value="12" />
                                    <Setter Property="Visibility" Value="{Binding Visibility}" />
                                    <Setter Property="Foreground" Value="{Binding TextColor}" />
                                    <Setter Property="Cursor" Value="Hand" />
                                    <!--  禁止水平卷軸自動捲動  -->
                                    <EventSetter Event="RequestBringIntoView" Handler="EventSetter_OnHandler" />
                                    <Style.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter Property="FontWeight" Value="Bold" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </TreeView.ItemContainerStyle>
                            <TreeView.ContextMenu>
                                <!--  右鍵選單  -->
                                <ContextMenu Visibility="Visible">
                                    <MenuItem
                                        x:Name="MenuSelectedItem"
                                        Padding="5,0,5,0"
                                        VerticalAlignment="Center"
                                        Click="MenuSelectedItem_OnClick"
                                        Cursor="Hand"
                                        Header="複製物件名" />
                                </ContextMenu>
                            </TreeView.ContextMenu>
                            <TreeView.ItemTemplate>
                                <HierarchicalDataTemplate DataType="{x:Type models:TreeNodeItem}" ItemsSource="{Binding Children}">
                                    <StackPanel Orientation="Horizontal">
                                        <svgc:SvgViewbox
                                            Width="12"
                                            Height="12"
                                            Margin="0,0,5,0"
                                            HorizontalAlignment="Left"
                                            Source="{Binding Icon}" />
                                        <TextBlock
                                            VerticalAlignment="Center"
                                            FontSize="12"
                                            Text="{Binding DisplayName}"
                                            ToolTip="{Binding DisplayName}" />
                                    </StackPanel>
                                </HierarchicalDataTemplate>
                            </TreeView.ItemTemplate>
                        </TreeView>
                        <Grid
                            x:Name="NoDataText"
                            Margin="0,100,0,5"
                            HorizontalAlignment="Stretch"
                            Background="White"
                            Cursor="Arrow">
                            <local:NoDataArea
                                x:Name="NoDataAreaText"
                                Margin="0"
                                HorizontalAlignment="Center"
                                ShowType="All" />
                        </Grid>
                        <Grid
                            Margin="0"
                            VerticalAlignment="Bottom"
                            Visibility="Hidden">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="4*" />
                                <ColumnDefinition Width="6*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid>
                                <ComboBox
                                    x:Name="CbTargetConnect"
                                    Height="26"
                                    VerticalAlignment="Bottom"
                                    HorizontalContentAlignment="Left"
                                    hc:InfoElement.Placeholder="目標連線"
                                    Cursor="Hand"
                                    DisplayMemberPath="ConnectName"
                                    IsTextSearchEnabled="True"
                                    SelectedValuePath="DbMasterConnectString"
                                    SelectionChanged="CbTargetConnect_OnSelectionChanged"
                                    Style="{StaticResource ComboBoxExtend}" />
                            </Grid>
                            <Grid Grid.Column="1" Margin="5,0,0,0">
                                <ComboBox
                                    x:Name="CbTargetDatabase"
                                    MinWidth="50"
                                    VerticalAlignment="Bottom"
                                    HorizontalContentAlignment="Left"
                                    hc:InfoElement.Placeholder="目標資料庫"
                                    Cursor="Hand"
                                    IsTextSearchEnabled="True"
                                    Style="{StaticResource ComboBoxExtend}" />
                            </Grid>
                            <Grid Grid.Column="2">
                                <!--  差異比較按鈕  -->
                                <Button
                                    x:Name="BtnCompare"
                                    Height="30"
                                    Margin="5,5,0,0"
                                    HorizontalAlignment="Right"
                                    hc:BorderElement.CornerRadius="6"
                                    hc:IconElement.Geometry="{StaticResource CompareGeometry}"
                                    Click="BtnCompare_OnClick"
                                    Content="差異比較"
                                    Cursor="Hand" />
                            </Grid>
                        </Grid>
                        <!--  資料載入Loading  -->
                        <hc:LoadingLine
                            x:Name="LoadingLine"
                            Margin="0,0,0,0"
                            Visibility="Collapsed" />
                    </Grid>
                </Border>
            </hc:SimplePanel>
        </DockPanel>

在這裡我沒有詳細介紹底層c#的相關程式碼,裡面邏輯有些複雜感興趣的可以去我的開源專案中學習。在上面的左側選單程式碼中,我們使用的不僅有TreeView控制元件、也有ContextMenuhc:LoadingLine等控制元件,還有自己寫的自定義控制元件。

其實WPF要比WinForm好用不少,不僅支援MVVM資料繫結還支援靈活的頁面渲染,自從用了WPF再也不用WinForm了。

今天分享暫時到這裡,下一篇講介紹DataGrid表格資料繫結及相關條件搜尋。下面是工具的開源地址,感興趣的可以Clone下來學習一下。碼磚不易,喜歡的麻煩點下Star.

開源地址

https://gitee.com/izhaofu/SmartSQL