// Disconnect protected void btnDisconnect_Click(object sender, EventArgs e) { string lastDatabase = this.mySqlCon.ConnStr; this.mySqlCon.ConnStr = null; this.frmMain.ChangeDatabaseName(null); this.btnDisconnect.Enabled = false; MessageBox.Show(FrmChangeDatabase.DatabaseName(lastDatabase) + " was disconnected!"); }
protected void ShowChangeDatabaseForm(bool firstTime) { string oldDatabase = this.mySqlCon.ConnStr; bool lastTmrEnabled = this.tmrCheckVPNConn.Enabled; this.frmChangeDatabase = new FrmChangeDatabase(this, firstTime, this.conStrs); if (!this.tmrCheckVPNConn.Enabled) { this.tmrCheckVPNConn.Enabled = true; } this.frmChangeDatabase.FormClosed += (s, arg) => this.ProceduresAfterNewDatabase(oldDatabase, lastTmrEnabled); this.frmChangeDatabase.ShowDialog(); }
//SELECT protected void btnSelectDatabase_Click(object sender, EventArgs e) { if (this.cbxChsDtBs.SelectedIndex < 0) { MessageBox.Show("Choose a valid database!"); return; } try { //change database string choosenConStr = this.conStrs[this.cbxChsDtBs.SelectedIndex]; string password = Encryption.Decrypt( choosenConStr.Substring(choosenConStr.LastIndexOf("Password="******"Password="******"Initial Catalog=") - 13); if (new System.Net.NetworkInformation.Ping().Send(dataSource).Status == System.Net.NetworkInformation.IPStatus.TimedOut) { throw new Exception("XXXX"); } //SEE IF OTHER FIELDS ARE RIGHT this.mySqlCon.ConnStr = conStr; try { //set last this.user.SetLastDatabase(this.conStrs[this.cbxChsDtBs.SelectedIndex]); } catch (Exception err2) {} } catch (Exception err) { MessageBox.Show("An error occurred when trying to connect to the chosen database!"); return; } //change database name in lbDatabase in the Main Form this.frmMain.ChangeDatabaseName(FrmChangeDatabase.DatabaseName(this.mySqlCon.ConnStr)); this.Close(); }
// Try Connection protected void btnTryConn_Click(object sender, EventArgs e) { string conStr; bool fromCbx = String.IsNullOrEmpty(this.txtDataSource.Text) || String.IsNullOrEmpty(this.txtInicialCatalog.Text) || String.IsNullOrEmpty(this.txtPassword.Text) || String.IsNullOrEmpty(this.txtUserID.Text); if (fromCbx) { //selected item in combobox if (this.cbxChsDtBs.SelectedIndex < 0) { MessageBox.Show((this.cbxChsDtBs.Items.Count > 0 ? "Select a databse in combobox or c" : "C") + "omplete all fields!"); return; } //Try connection with database string choosenConStr = this.conStrs[this.cbxChsDtBs.SelectedIndex]; string password = Encryption.Decrypt( choosenConStr.Substring(choosenConStr.LastIndexOf("Password="******"Password="******"Initial Catalog=") - 13); } else { dataSource = this.txtDataSource.Text; } try { //SPENDS JUST 5 SECONDS TO KNOW IF DATABASE IS CONNECTED OR NOT if (new System.Net.NetworkInformation.Ping().Send(dataSource).Status == System.Net.NetworkInformation.IPStatus.TimedOut) { throw new Exception("XXXX"); } //SEE IF OTHER FIELDS ARE RIGHT SqlConnection con = new SqlConnection(conStr); if (con != null) { con.Open(); worked = con.IsConnected(); } } catch (Exception err) { worked = false; } string databaseName = FrmChangeDatabase.DatabaseName(conStr); if (worked) { MessageBox.Show("Successfully connection with " + databaseName + "!", "Succefully connection", MessageBoxButtons.OK); } else { MessageBox.Show("Unsuccessfully connection with " + databaseName + "!", "Unsuccefully connection", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// NEW DATABASE protected void btnAddNewDatabase_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.txtDataSource.Text) || String.IsNullOrEmpty(this.txtInicialCatalog.Text) || String.IsNullOrEmpty(this.txtPassword.Text) || String.IsNullOrEmpty(this.txtUserID.Text)) { MessageBox.Show("Complete all fields!"); return; } string conStr = this.GetConnStrWithoutPasswFromFields(); string conStrComplete = conStr + Encryption.Encrypt(this.txtPassword.Text); if (this.updating && this.conStrs[this.cbxChsDtBs.SelectedIndex] == conStrComplete) { MessageBox.Show("Update to a new database!"); return; } if (this.updating) { DialogResult result = MessageBox.Show("Are you sure you want to update this database?", "Attention!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.No) { return; } } string conStrNoEncryp = conStr + this.txtPassword.Text; bool canConnect = true; SqlConnection auxCon = null; try { //SPENDS JUST 5 SECONDS TO KNOW IF DATABASE IS CONNECTED OR NOT if (new System.Net.NetworkInformation.Ping().Send(this.txtDataSource.Text).Status == System.Net.NetworkInformation.IPStatus.TimedOut) { throw new Exception("XXXX"); } //SEE IF OTHER FIELDS ARE RIGHT if (this.user == null) { //connect to new database this.mySqlCon.ConnStr = conStrNoEncryp; } else { auxCon = new SqlConnection(); auxCon.ConnectionString = conStrNoEncryp; auxCon.Open(); } }catch (Exception err) { DialogResult result = DialogResult.No; if (this.user == null) { MessageBox.Show("Cannot connect to this database!", "Connection Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { result = MessageBox.Show("Cannot connect to this database!\r\nDo you want to add it in your databases even so?", "Attention!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } if (result == DialogResult.No) { return; } canConnect = false; } if (this.user == null) { //change database name in lbDatabase in the Main Form this.frmMain.ChangeDatabaseName(FrmChangeDatabase.DatabaseName(this.mySqlCon.ConnStr)); this.Close(); return; } //checks if this database already exists if (this.DatabaseExists(conStrComplete)) { MessageBox.Show("This database already exists!"); return; } if (this.updating) { //delete old database this.user.DeleteDatabase(this.conStrs[this.cbxChsDtBs.SelectedIndex]); } //add new string connection this.user.AddDatabase(conStrComplete); if (this.updating) { //variables this.conStrs[this.cbxChsDtBs.SelectedIndex] = conStrComplete; this.cbxChsDtBs.Items[this.cbxChsDtBs.SelectedIndex] = FrmChangeDatabase.DatabaseName(conStrComplete); MessageBox.Show("Database updated!"); this.btnCancel.PerformClick(); } else { //variables this.conStrs.Add(conStrComplete); this.cbxChsDtBs.Items.Add(DatabaseName(conStrComplete)); this.cbxChsDtBs.SelectedIndex = this.cbxChsDtBs.Items.Count - 1; if (canConnect) { this.mySqlCon.Conn = auxCon; this.frmMain.ChangeDatabaseName(FrmChangeDatabase.DatabaseName(this.mySqlCon.ConnStr)); this.Close(); return; } else { MessageBox.Show("New database added!"); //reset textBoxes this.txtDataSource.Text = ""; this.txtInicialCatalog.Text = ""; this.txtPassword.Text = ""; this.txtUserID.Text = ""; } } }