Add QueryWindow
This commit is contained in:
@ -45,6 +45,8 @@ namespace Elwig.Helpers {
|
|||||||
public DateTime SavedLastWriteTime { get; private set; }
|
public DateTime SavedLastWriteTime { get; private set; }
|
||||||
public bool HasBackendChanged => SavedLastWriteTime != LastWriteTime;
|
public bool HasBackendChanged => SavedLastWriteTime != LastWriteTime;
|
||||||
|
|
||||||
|
public static string ConnectionString => $"Data Source=\"{App.Config.DatabaseFile}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default";
|
||||||
|
|
||||||
public AppDbContext() {
|
public AppDbContext() {
|
||||||
if (App.Config.DatabaseLog != null) {
|
if (App.Config.DatabaseLog != null) {
|
||||||
try {
|
try {
|
||||||
@ -62,7 +64,7 @@ namespace Elwig.Helpers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||||
optionsBuilder.UseSqlite($"Data Source=\"{App.Config.DatabaseFile}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default");
|
optionsBuilder.UseSqlite(ConnectionString);
|
||||||
optionsBuilder.UseLazyLoadingProxies();
|
optionsBuilder.UseLazyLoadingProxies();
|
||||||
optionsBuilder.LogTo(Log, LogLevel.Information);
|
optionsBuilder.LogTo(Log, LogLevel.Information);
|
||||||
base.OnConfiguring(optionsBuilder);
|
base.OnConfiguring(optionsBuilder);
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
<Style TargetType="Button">
|
<Style TargetType="Button">
|
||||||
<Setter Property="FontSize" Value="14"/>
|
<Setter Property="FontSize" Value="14"/>
|
||||||
<Setter Property="Padding" Value="9,3"/>
|
<Setter Property="Padding" Value="9,3"/>
|
||||||
<Setter Property="Height" Value="27"/>
|
<Setter Property="Height" Value="32"/>
|
||||||
<Setter Property="Width" Value="200"/>
|
<Setter Property="Width" Value="200"/>
|
||||||
</Style>
|
</Style>
|
||||||
</Window.Resources>
|
</Window.Resources>
|
||||||
@ -47,8 +47,10 @@
|
|||||||
<Button x:Name="DeliveryListButton" Content="Lieferungungen" Click="DeliveryListButton_Click" IsEnabled="False"
|
<Button x:Name="DeliveryListButton" Content="Lieferungungen" Click="DeliveryListButton_Click" IsEnabled="False"
|
||||||
Margin="50,280,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
Margin="50,280,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
||||||
<Button x:Name="PdfButton" Content="PDF Erzeugen" Click="PdfButton_Click" Tag="Print"
|
<Button x:Name="PdfButton" Content="PDF Erzeugen" Click="PdfButton_Click" Tag="Print"
|
||||||
Margin="50,320,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
Margin="260,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
||||||
<Button x:Name="TestWindowButton" Content="Test Fenster" Click="TestWindowButton_Click"
|
<Button x:Name="TestWindowButton" Content="Test Fenster" Click="TestWindowButton_Click"
|
||||||
Margin="50,360,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
Margin="260,200,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
||||||
|
<Button x:Name="QueryWindowButton" Content="Datenbankabfragen" Click="QueryWindowButton_Click"
|
||||||
|
Margin="260,240,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
@ -47,5 +47,10 @@ namespace Elwig.Windows {
|
|||||||
var w = new TestWindow();
|
var w = new TestWindow();
|
||||||
w.Show();
|
w.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void QueryWindowButton_Click(object sender, RoutedEventArgs evt) {
|
||||||
|
var w = new QueryWindow();
|
||||||
|
w.Show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
Elwig/Windows/QueryWindow.xaml
Normal file
19
Elwig/Windows/QueryWindow.xaml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<Window x:Class="Elwig.Windows.QueryWindow"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
Title="Datenbankabfragen - Elwig" Height="450" Width="800">
|
||||||
|
<Grid>
|
||||||
|
<TextBox x:Name="QueryInput" Text="SELECT * FROM v_delivery"
|
||||||
|
AcceptsReturn="True" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap"
|
||||||
|
HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="10,10,120,10" Height="100"
|
||||||
|
FontFamily="Cascadia Code Light" FontSize="13"/>
|
||||||
|
<Button x:Name="QueryButton" Content="Abfragen"
|
||||||
|
HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10,10,10,10"
|
||||||
|
Click="QueryButton_Click" Height="100" Width="100"
|
||||||
|
FontSize="14"/>
|
||||||
|
<DataGrid x:Name="DataList"
|
||||||
|
AutoGenerateColumns="False" HeadersVisibility="Column" IsReadOnly="True" GridLinesVisibility="None" SelectionMode="Single"
|
||||||
|
CanUserDeleteRows="False" CanUserResizeRows="False" CanUserAddRows="False"
|
||||||
|
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,120,10,10"/>
|
||||||
|
</Grid>
|
||||||
|
</Window>
|
56
Elwig/Windows/QueryWindow.xaml.cs
Normal file
56
Elwig/Windows/QueryWindow.xaml.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
using Elwig.Helpers;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
|
|
||||||
|
namespace Elwig.Windows {
|
||||||
|
public partial class QueryWindow : Window {
|
||||||
|
public QueryWindow() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void QueryButton_Click(object sender, RoutedEventArgs evt) {
|
||||||
|
try {
|
||||||
|
ExecuteQuery(QueryInput.Text);
|
||||||
|
} catch (Exception e) {
|
||||||
|
MessageBox.Show(e.Message, "Fehler beim Ausführen", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ExecuteQuery(string sqlQuery) {
|
||||||
|
var rows = new List<object[]>();
|
||||||
|
IList<DbColumn> header;
|
||||||
|
|
||||||
|
using (var cnx = new SqliteConnection(AppDbContext.ConnectionString)) {
|
||||||
|
cnx.Open();
|
||||||
|
var cmd = cnx.CreateCommand();
|
||||||
|
cmd.CommandText = sqlQuery;
|
||||||
|
using var reader = cmd.ExecuteReader();
|
||||||
|
header = reader.GetColumnSchema();
|
||||||
|
while (reader.Read()) {
|
||||||
|
var values = new object[reader.FieldCount];
|
||||||
|
reader.GetValues(values);
|
||||||
|
rows.Add(values);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var styleRight = new Style();
|
||||||
|
styleRight.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));
|
||||||
|
|
||||||
|
DataList.Columns.Clear();
|
||||||
|
for (int i = 0; i < header.Count; i++) {
|
||||||
|
var h = header[i];
|
||||||
|
DataList.Columns.Add(new DataGridTextColumn {
|
||||||
|
Header = h.ColumnName,
|
||||||
|
Binding = new Binding($"[{i}]"),
|
||||||
|
CellStyle = (h.DataTypeName == "INTEGER" || h.DataTypeName == "REAL") ? styleRight : null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
DataList.ItemsSource = rows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user