Skip to content

Commit

Permalink
Fix issue listing databases in Connect dialog
Browse files Browse the repository at this point in the history
Connect dialog will fail to list databases if the selected database doesn't exist as it uses it as the initial catalog in the connection string to list the DBs.
Refactoring
#390
  • Loading branch information
DavidWiseman committed Oct 15, 2022
1 parent 7792b0b commit adacd98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
8 changes: 4 additions & 4 deletions DBADashSharedGUI/DBConnection.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions DBADashSharedGUI/DBConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,34 @@ public string ConnectionStringWithoutInitialCatalog
}
}

private void chkIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
private void ChkIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
{
pnlAuth.Enabled = !chkIntegratedSecurity.Checked;
}

private void bttnCancel_Click(object sender, EventArgs e)
private void BttnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}

public void testConnection(string connectionString)
public static void TestConnection(string connectionString)
{
SqlConnection cn = new SqlConnection(connectionString);
using (cn)
{
cn.Open();
}

using var cn = new SqlConnection(connectionString);
cn.Open();
}

private void bttnConnect_Click(object sender, EventArgs e)
private void BttnConnect_Click(object sender, EventArgs e)
{
try
{
this.Cursor = Cursors.WaitCursor;
if (ValidateInitialCatalog)
{
testConnection(ConnectionString);
TestConnection(ConnectionString);
}
else
{
testConnection(ConnectionStringWithoutInitialCatalog); // Try without initial catalog as DB might not have been created yet
TestConnection(ConnectionStringWithoutInitialCatalog); // Try without initial catalog as DB might not have been created yet
}
}
catch (Exception ex)
Expand All @@ -118,12 +114,12 @@ private void DBConnection_Load(object sender, EventArgs e)
}
}

private void cboDatabase_Dropdown(object sender, EventArgs e)
private void CboDatabase_Dropdown(object sender, EventArgs e)
{
try
{
cboDatabase.Items.Clear();
var DBs = GetDatabases(ConnectionString);
var DBs = GetDatabases(ConnectionStringWithoutInitialCatalog);
foreach(string db in DBs)
{
cboDatabase.Items.Add(db);
Expand All @@ -137,10 +133,10 @@ private void cboDatabase_Dropdown(object sender, EventArgs e)

}

public List<string> GetDatabases(string ConnectionString)
public static List<string> GetDatabases(string ConnectionString)
{
using (var cn = new SqlConnection(ConnectionString))
using (SqlCommand cmd = new SqlCommand("SELECT name FROM sys.databases WHERE state=0", cn))
using (SqlCommand cmd = new("SELECT name FROM sys.databases WHERE state=0", cn))
{
cn.Open();
var DBs = new List<string>();
Expand Down

0 comments on commit adacd98

Please sign in to comment.