QueryWindow: Allow users to export query result to csv file
All checks were successful
Test / Run tests (push) Successful in 1m47s
All checks were successful
Test / Run tests (push) Successful in 1m47s
This commit is contained in:
@@ -5,27 +5,35 @@
|
||||
Title="Datenbankabfragen - Elwig" Height="450" Width="800" MinWidth="400" MinHeight="300">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="1*" MinHeight="100"/>
|
||||
<RowDefinition Height="5"/>
|
||||
<RowDefinition Height="3*" MinHeight="100"/>
|
||||
<RowDefinition Height="1*" MinHeight="50"/>
|
||||
<RowDefinition Height="5"/>
|
||||
<RowDefinition Height="1*" MinHeight="50"/>
|
||||
<RowDefinition Height="5"/>
|
||||
<RowDefinition Height="6*" MinHeight="100"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBox x:Name="QueryInput" Text="SELECT * FROM v_delivery"
|
||||
<TextBox x:Name="QueryInput" Text="SELECT * FROM v_member" Grid.Row="1" Grid.RowSpan="3"
|
||||
AcceptsReturn="True" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap"
|
||||
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,120,5"
|
||||
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,5,120,5"
|
||||
FontFamily="Cascadia Code Light" FontSize="13">
|
||||
<TextBox.InputBindings>
|
||||
<KeyBinding Key="Return" Modifiers="Control" Command="{Binding EnterCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:QueryWindow}}}" />
|
||||
<KeyBinding Key="Return" Modifiers="Control" Command="{Binding EnterCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:QueryWindow}}}" />
|
||||
<KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:QueryWindow}}}" />
|
||||
</TextBox.InputBindings>
|
||||
</TextBox>
|
||||
<Button x:Name="QueryButton" Content="Abfragen"
|
||||
HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="10,10,10,5"
|
||||
<Button x:Name="QueryButton" Content="Abfragen" Grid.Row="1"
|
||||
HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="10,5,10,0"
|
||||
Click="QueryButton_Click" Width="100"
|
||||
FontSize="14"/>
|
||||
<Button x:Name="SaveButton" Content="Speichern" Grid.Row="3"
|
||||
HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="10,0,10,5"
|
||||
Click="SaveButton_Click" Width="100"
|
||||
FontSize="14"/>
|
||||
|
||||
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
|
||||
<GridSplitter Grid.Row="4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
|
||||
|
||||
<DataGrid x:Name="DataList" Grid.Row="2"
|
||||
<DataGrid x:Name="DataList" Grid.Row="5"
|
||||
AutoGenerateColumns="False" HeadersVisibility="Column" IsReadOnly="True" GridLinesVisibility="None" SelectionMode="Extended"
|
||||
CanUserDeleteRows="False" CanUserResizeRows="False" CanUserAddRows="False"
|
||||
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,5,10,10"/>
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Helpers.Export;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
@@ -13,7 +16,12 @@ namespace Elwig.Windows {
|
||||
|
||||
private ICommand? _enterCommand;
|
||||
public ICommand EnterCommand => _enterCommand ??= new ActionCommand(async () => {
|
||||
await ExecuteQuery();
|
||||
await DisplayQuery();
|
||||
});
|
||||
|
||||
private ICommand? _saveCommand;
|
||||
public ICommand SaveCommand => _saveCommand ??= new ActionCommand(async () => {
|
||||
await SaveQuery();
|
||||
});
|
||||
|
||||
|
||||
@@ -22,33 +30,45 @@ namespace Elwig.Windows {
|
||||
}
|
||||
|
||||
private async void QueryButton_Click(object sender, RoutedEventArgs evt) {
|
||||
await ExecuteQuery();
|
||||
await DisplayQuery();
|
||||
}
|
||||
|
||||
private async Task ExecuteQuery() {
|
||||
private async void SaveButton_Click(object sender, RoutedEventArgs evt) {
|
||||
await SaveQuery();
|
||||
}
|
||||
|
||||
private async Task DisplayQuery() {
|
||||
try {
|
||||
await ExecuteQuery(QueryInput.Text);
|
||||
Mouse.OverrideCursor = Cursors.Wait;
|
||||
await DisplayQuery(QueryInput.Text);
|
||||
Mouse.OverrideCursor = null;
|
||||
} catch (Exception e) {
|
||||
Mouse.OverrideCursor = null;
|
||||
MessageBox.Show(e.Message, "Fehler beim Ausführen", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExecuteQuery(string sqlQuery) {
|
||||
private async Task SaveQuery() {
|
||||
await SaveQuery(QueryInput.Text);
|
||||
}
|
||||
|
||||
private static async Task<(IList<DbColumn>, IEnumerable<object[]>)> ExecuteQuery(string sqlQuery) {
|
||||
var rows = new List<object[]>();
|
||||
IList<DbColumn> header;
|
||||
|
||||
using (var cnx = await AppDbContext.ConnectAsync()) {
|
||||
using var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = sqlQuery;
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
header = await reader.GetColumnSchemaAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
var values = new object[reader.FieldCount];
|
||||
reader.GetValues(values);
|
||||
rows.Add(values);
|
||||
}
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
using var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = sqlQuery;
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
var header = await reader.GetColumnSchemaAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
var values = new object[reader.FieldCount];
|
||||
reader.GetValues(values);
|
||||
rows.Add(values);
|
||||
}
|
||||
return (header, rows);
|
||||
}
|
||||
|
||||
private async Task DisplayQuery(string sqlQuery) {
|
||||
var (header, rows) = await ExecuteQuery(sqlQuery);
|
||||
var styleRight = new Style();
|
||||
styleRight.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));
|
||||
|
||||
@@ -63,5 +83,27 @@ namespace Elwig.Windows {
|
||||
}
|
||||
DataList.ItemsSource = rows;
|
||||
}
|
||||
|
||||
private static async Task SaveQuery(string sqlQuery) {
|
||||
var d = new SaveFileDialog() {
|
||||
FileName = $"Abfrage.csv",
|
||||
DefaultExt = "csv",
|
||||
Filter = "CSV-Datei (*.csv)|*.csv",
|
||||
Title = $"Datenbank Abfrage speichern unter - Elwig"
|
||||
};
|
||||
if (d.ShowDialog() == true) {
|
||||
Mouse.OverrideCursor = Cursors.Wait;
|
||||
await Task.Run(async () => {
|
||||
try {
|
||||
var (header, rows) = await ExecuteQuery(sqlQuery);
|
||||
using var csv = new CsvSimple(d.FileName, ';', Utils.UTF8BOM);
|
||||
await csv.ExportAsync(rows.Prepend([.. header.Select(h => h.ColumnName)]));
|
||||
} catch (Exception exc) {
|
||||
MessageBox.Show(exc.Message, "Fehler beim Ausführen", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
});
|
||||
Mouse.OverrideCursor = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user