-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
57 lines (47 loc) · 1.26 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Npgsql;
using System;
using System.Windows;
namespace DbGui
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var connString = $"Host={host.Text};Username={username.Text};Password={password.Text};Database={database.Text}";
await using var conn = new NpgsqlConnection(connString);
await conn.OpenAsync();
uint countResults = 0;
string queryResultString = string.Empty;
await using (var cmd = new NpgsqlCommand(query.Text, conn))
{
var dataReader = await cmd.ExecuteReaderAsync();
while (await dataReader.ReadAsync())
{
if (dataReader.FieldCount > 0)
{
countResults++;
for (int i = 0; i < dataReader.FieldCount; i++)
{
queryResultString += dataReader[i].ToString();
if (i == dataReader.FieldCount - 2)
{
queryResultString += ", ";
}
}
queryResultString += Environment.NewLine;
}
}
}
labelQueryStatus.Content = "Query completed.";
labelCountResults.Content = countResults.ToString() + " results.";
results.Text = queryResultString;
}
}
}