using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Data.Sql; using System.Data.SqlClient; using System.Security.Principal; namespace sharpcomparer.controls { public partial class Database : UserControl { public string ConnectionString { get { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = DataSource; builder.IntegratedSecurity = IntegratedSecurity; builder.UserID = UserID; if (!string.IsNullOrEmpty(Password)) { builder.Password = Password; } if (!string.IsNullOrEmpty(InitialCatalog)) { builder.InitialCatalog = InitialCatalog; } return builder.ConnectionString; } } public string RegisteredName { get { return textBoxName.Text; } } public string DataSource { get { return comboServerName.Text; } } public bool IntegratedSecurity { get { return comboAuthentication.SelectedIndex == 0; } } public string UserID { get { return textUsername.Text; } } public string Password { get { return textPassword.Text; } } public string InitialCatalog { get { return comboBoxDatabase.Text; } } public Database() { InitializeComponent(); comboAuthentication.SelectedIndex = 0; } private void comboBoxServerName_DropDown(object sender, EventArgs e) { comboServerName.DataSource = null; SqlDataSourceEnumerator servers = SqlDataSourceEnumerator.Instance; DataTable serversTable = servers.GetDataSources(); comboServerName.DataSource = serversTable; comboServerName.DisplayMember = "ServerName"; } private void comboBoxDatabase_DropDown(object sender, EventArgs e) { comboBoxDatabase.DataSource = null; if (!string.IsNullOrEmpty(comboServerName.Text)) { SqlConnection connection = new SqlConnection(ConnectionString); try { connection.Open(); DataTable data = connection.GetSchema("Databases"); connection.Close(); List databases = new List(); foreach (DataRow row in data.Rows) { databases.Add((string)row["database_name"]); } databases.Sort(); comboBoxDatabase.DataSource = databases; } catch (Exception) { MessageBox.Show("Unable to get databases list", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void buttonTest_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(ConnectionString); try { connection.Open(); MessageBox.Show("Connection succeded", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception) { MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (connection.State != ConnectionState.Closed) connection.Close(); } } private void comboAuthentication_SelectedIndexChanged(object sender, EventArgs e) { textUsername.Enabled = comboAuthentication.SelectedIndex == 1; textPassword.Enabled = comboAuthentication.SelectedIndex == 1; checkBoxRemember.Enabled = comboAuthentication.SelectedIndex == 1; if (comboAuthentication.SelectedIndex == 0) { textUsername.Text = WindowsIdentity.GetCurrent().Name; } else { textUsername.Text = string.Empty; } textPassword.Text = string.Empty; } private bool TestEnabled { get { return !string.IsNullOrEmpty(textUsername.Text) && !string.IsNullOrEmpty(comboServerName.Text) && !string.IsNullOrEmpty(comboBoxDatabase.Text); } } private void comboServerName_TextChanged(object sender, EventArgs e) { comboBoxDatabase.Enabled = !string.IsNullOrEmpty(comboServerName.Text); } private void comboBoxDatabase_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxDatabase.SelectedIndex >= 0) { textBoxName.Text = comboBoxDatabase.Text; } buttonTest.Enabled = TestEnabled; } public new bool Validate() { return TestEnabled; } } }