Skip to content

Commit df546e9

Browse files
authored
Add server settings (#3)
* Add server selection * Fix height and add message and password box
1 parent abda4b0 commit df546e9

25 files changed

+348
-85
lines changed

Sql Widget/App.config

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="Sql_Widget.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
6+
</sectionGroup>
7+
</configSections>
38
<startup>
49
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
510
</startup>
11+
<userSettings>
12+
<Sql_Widget.Properties.Settings>
13+
<setting name="Server" serializeAs="String">
14+
<value />
15+
</setting>
16+
<setting name="UserName" serializeAs="String">
17+
<value />
18+
</setting>
19+
<setting name="Password" serializeAs="String">
20+
<value />
21+
</setting>
22+
</Sql_Widget.Properties.Settings>
23+
</userSettings>
624
</configuration>

Sql Widget/Helper/Converters.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Sql_Widget.Helper
66
{
7+
[ValueConversion(typeof(bool), typeof(Brushes))]
78
public class ResultToColorConverter : IValueConverter
89
{
910
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
@@ -30,4 +31,27 @@ public object ConvertBack(object value, Type targetType, object parameter, Syste
3031
return false;
3132
}
3233
}
34+
35+
[ValueConversion(typeof(bool), typeof(bool))]
36+
public class InverseBooleanConverter : IValueConverter
37+
{
38+
#region IValueConverter Members
39+
40+
public object Convert(object value, Type targetType, object parameter,
41+
System.Globalization.CultureInfo culture)
42+
{
43+
if (targetType != typeof(bool))
44+
throw new InvalidOperationException("The target must be a boolean");
45+
46+
return !(bool)value;
47+
}
48+
49+
public object ConvertBack(object value, Type targetType, object parameter,
50+
System.Globalization.CultureInfo culture)
51+
{
52+
throw new NotSupportedException();
53+
}
54+
55+
#endregion
56+
}
3357
}

Sql Widget/Helper/Extensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace Sql_Widget.Helper
6+
{
7+
public static class Extensions
8+
{
9+
public static async Task TimeoutAfter(this Task task, int timeout)
10+
{
11+
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
12+
{
13+
14+
var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
15+
if (completedTask == task)
16+
{
17+
timeoutCancellationTokenSource.Cancel();
18+
await task; // Very important in order to propagate exceptions
19+
}
20+
else
21+
{
22+
throw new TimeoutException("The operation has timed out.");
23+
}
24+
}
25+
}
26+
}
27+
}

Sql Widget/Models/DBModel.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Collections.Generic;
2-
using System.Data;
1+
using Sql_Widget.Helper;
2+
using System;
3+
using System.Collections.Generic;
34
using System.Data.SqlClient;
45
using System.Linq;
56
using System.Threading.Tasks;
@@ -19,22 +20,37 @@ public void InvalidateCache()
1920
TableColumnsModel.InvalidateCache();
2021
}
2122

22-
public Task<List<string>> GetAllDBs() => Task.Run(() => GetDbs());
23-
24-
private List<string> GetDbs()
23+
public async Task<List<string>> GetAllDBs()
2524
{
2625
if (!_cachedDBs.Any())
27-
using (SqlConnection con = new SqlConnection(QueryModel.ConnectionString(_dbName)))
26+
using (SqlConnection con = new SqlConnection(QueryModel.GetConnectionString(_dbName)))
2827
{
29-
con.Open();
28+
await con.OpenAsync();
3029
using (SqlCommand cmd = new SqlCommand(_selectAllDBsString, con))
3130
{
32-
using (IDataReader dr = cmd.ExecuteReader())
33-
while (dr.Read())
34-
_cachedDBs.Add(dr[0].ToString());
31+
using (var dr = await cmd.ExecuteReaderAsync())
32+
while (await dr.ReadAsync())
33+
_cachedDBs.Add(await dr.GetTextReader(0).ReadToEndAsync());
3534
}
3635
}
3736
return _cachedDBs;
3837
}
38+
39+
public async Task<bool> CheckConnection()
40+
{
41+
using (SqlConnection con = new SqlConnection(QueryModel.GetConnectionString(_dbName)))
42+
{
43+
try
44+
{
45+
await con.OpenAsync().TimeoutAfter(10000);
46+
con.Close();
47+
return true;
48+
}
49+
catch(Exception ex)
50+
{
51+
return false;
52+
}
53+
}
54+
}
3955
}
4056
}

Sql Widget/Models/QueryModel.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@ namespace Sql_Widget.Models
66
{
77
class QueryModel
88
{
9-
public static string ConnectionString(string dbName) => $"server=.;Database={dbName};Integrated Security=true";
9+
public static string GetConnectionString(string dbName)
10+
{
11+
var server = Properties.Settings.Default.Server;
12+
var credentials = string.IsNullOrWhiteSpace(Properties.Settings.Default.UserName)
13+
? "Integrated Security=true"
14+
: $"User Id={Properties.Settings.Default.UserName}; Password={Properties.Settings.Default.Password}";
15+
16+
return $"server={server};Database={dbName};{credentials}";
17+
}
18+
1019

1120
public DataTable Execute(string dbName, string query)
1221
{
13-
using (SqlConnection con = new SqlConnection(ConnectionString(dbName)))
22+
using (SqlConnection con = new SqlConnection(GetConnectionString(dbName)))
1423
{
1524
//Thread.Sleep(1000);
1625
DataTable dt = new DataTable();

Sql Widget/Models/TableColumnsModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private static List<TableColumn> GetColumnsFromCache(string db, string table)
2222
if (!_cachedColumns.ContainsKey(key))
2323
{
2424
List<TableColumn> columns = new List<TableColumn>();
25-
using (SqlConnection con = new SqlConnection(QueryModel.ConnectionString(db)))
25+
using (SqlConnection con = new SqlConnection(QueryModel.GetConnectionString(db)))
2626
{
2727
con.Open();
2828
using (SqlCommand cmd = new SqlCommand(GetAllTableColumnsQuery(table), con))

Sql Widget/Models/TablesModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static List<string> GetTablesFromCache(string dbName)
2020
if (!_cachedTables.ContainsKey(dbName))
2121
{
2222
List<string> tables = new List<string>();
23-
using (SqlConnection con = new SqlConnection(QueryModel.ConnectionString(dbName)))
23+
using (SqlConnection con = new SqlConnection(QueryModel.GetConnectionString(dbName)))
2424
{
2525
con.Open();
2626
DataTable schema = con.GetSchema("Tables");

Sql Widget/Properties/Settings.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
3-
<Profiles>
4-
<Profile Name="(Default)" />
5-
</Profiles>
6-
<Settings />
2+
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Sql_Widget.Properties" GeneratedClassName="Settings">
3+
<Profiles />
4+
<Settings>
5+
<Setting Name="Server" Type="System.String" Scope="User">
6+
<Value Profile="(Default)" />
7+
</Setting>
8+
<Setting Name="UserName" Type="System.String" Scope="User">
9+
<Value Profile="(Default)" />
10+
</Setting>
11+
<Setting Name="Password" Type="System.String" Scope="User">
12+
<Value Profile="(Default)" />
13+
</Setting>
14+
</Settings>
715
</SettingsFile>

Sql Widget/Sql Widget.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Generator>MSBuild:Compile</Generator>
8080
<SubType>Designer</SubType>
8181
</ApplicationDefinition>
82+
<Compile Include="Helper\Extensions.cs" />
8283
<Compile Include="Views\Controls\FavoriteControl.xaml.cs">
8384
<DependentUpon>FavoriteControl.xaml</DependentUpon>
8485
</Compile>
@@ -88,6 +89,9 @@
8889
<Compile Include="Views\Controls\SelectControl.xaml.cs">
8990
<DependentUpon>SelectControl.xaml</DependentUpon>
9091
</Compile>
92+
<Compile Include="Views\Controls\ServerControl.xaml.cs">
93+
<DependentUpon>ServerControl.xaml</DependentUpon>
94+
</Compile>
9195
<Compile Include="Views\Controls\WhereControl.xaml.cs">
9296
<DependentUpon>WhereControl.xaml</DependentUpon>
9397
</Compile>
@@ -123,6 +127,10 @@
123127
<SubType>Designer</SubType>
124128
<Generator>MSBuild:Compile</Generator>
125129
</Page>
130+
<Page Include="Views\Controls\ServerControl.xaml">
131+
<SubType>Designer</SubType>
132+
<Generator>MSBuild:Compile</Generator>
133+
</Page>
126134
<Page Include="Views\Controls\WhereControl.xaml">
127135
<SubType>Designer</SubType>
128136
<Generator>MSBuild:Compile</Generator>
@@ -178,7 +186,9 @@
178186
<AppDesigner Include="Properties\" />
179187
</ItemGroup>
180188
<ItemGroup>
181-
<None Include="App.config" />
189+
<None Include="App.config">
190+
<SubType>Designer</SubType>
191+
</None>
182192
</ItemGroup>
183193
<ItemGroup>
184194
<Resource Include="Images\sql.ico" />

0 commit comments

Comments
 (0)