/// <summary> /// /// </summary> /// <param name="result"></param> /// <returns></returns> private ServerInstance LocalWriteConnectionString(SQLServerInstanceResult result) { MattimonSQLite.SQLiteClientDatabase db = GetLocalDatabase(); db.CreateConnectionStringTable(); string servername = result.ServerName; string instancename = result.InstanceName; string connectionstring = result.ConnectionString; string username = result.Username; string version = result.Version; bool monitored = true; db.InsertConnectionString(connectionstring, servername, instancename, username, version, monitored); DataTable dt = (DataTable)db.SelectConnectionString(servername, instancename); return(new ServerInstance { ServerName = dt.Rows[0]["servername"] as string, InstanceName = dt.Rows[0]["instancename"] as string, ConnectionString = dt.Rows[0]["connectionstring"] as string, User = dt.Rows[0]["user"] as string, Clustered = result.Clustered, Monitored = Convert.ToBoolean(dt.Rows[0]["monitored"]), Version = result.Version }); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnCancel_Click(object sender, EventArgs e) { if (BackgroundWorkerEnumerateSQLInstances.IsBusy) { BackgroundWorkerEnumerateSQLInstances.CancelAsync(); BackgroundWorkerEnumerateSQLInstances.Dispose(); } mDialogResult = DialogResult.Cancel; mSqlserverInstanceResult = SQLServerInstanceResult.CancelResult; Close(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSqlSrv_AddNew_Click(object sender, EventArgs e) { while (true) { SQLServerInstanceDialog serverInstanceDialog = new SQLServerInstanceDialog(); SQLServerInstanceResult result = serverInstanceDialog.Show(this); if (result.DialogResult == DialogResult.OK) { bool entryExists = false; try { entryExists = ConnectionStringEntryExists(result.ServerName, result.InstanceName); } catch { } if (entryExists) { if (GUI.BitscoreForms.BitscoreMessageBox.Show(this, "The connection string you've selected is already set in the Mattimon SQL Service.", Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK) { return; } continue; } if (GUI.BitscoreForms.BitscoreMessageBox.Show(this, "You're about to add " + result.ServerName + (result.InstanceName != "" ? "//" + result.InstanceName : "") + " in the Mattimon Service.\nIs that correct?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { continue; } lblStatus.Text = "Posting Server Instances..."; try { LocalWriteConnectionString(result); } catch (Exception ex) { lblStatus.Text = "Failed"; GUI.BitscoreForms.BitscoreMessageBox.Show(this, Static.UserEndErrorMessages.SQLITE_ERROR_WRITE_CONNECTION_STRINGS + ex.Message + "\n\n" + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); lblStatus.Text = "Ready"; return; } DeviceServerObjects dso = new SQLServerObjectProvider().GetDeviceServerObjects( new ServerInstance[] { new ServerInstance { ConnectionString = result.ConnectionString }, }, GetDeviceID(), out Exception outex); if (outex != null) { GUI.BitscoreForms.BitscoreMessageBox.Show(this, outex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MattimonSqlServerPostDataWorker.RunWorkerAsync(dso); break; } break; } }
/// <summary> /// Thread /// </summary> /// <returns></returns> private void Accept() { if (cboServers.SelectedIndex < 1 || txtPwrd.TextLength < 1 || txtUser.TextLength < 1) { GUI.BitscoreForms.BitscoreMessageBox.Show(this, "Required information not provided.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } lblStatus.Hide(); String cs = CreateConnectionString( cboServers.SelectedItem.ToString(), txtUser.Text.Trim(), txtPwrd.Text, "master"); new System.Threading.Thread(() => { Invoke((MethodInvoker) delegate() { ApplyCursorToControlTree(Cursors.AppStarting); }); Invoke((MethodInvoker) delegate() { pnlSqlCred.Enabled = false; }); Invoke((MethodInvoker) delegate() { btnTestConnection.Enabled = false; }); Invoke((MethodInvoker) delegate() { btnAccept.Enabled = false; }); using (SqlConnection sqlconnection = new SqlConnection(cs)) { try { sqlconnection.Open(); sqlconnection.Close(); // Acceptable connection close the dialogue -- thread safe. Invoke((MethodInvoker) delegate() { mSelectedConnectionString = cs; string srvname, insname, srvversion; bool clustered; try { string[] parameters = mServerInstanceSelection.Split(';'); srvname = parameters[0]; insname = parameters[1]; clustered = Convert.ToBoolean(parameters[2]); srvversion = parameters[3]; mDialogResult = DialogResult.OK; mSqlserverInstanceResult = new SQLServerInstanceResult(mDialogResult, srvname, insname, clustered, srvversion, mSqlConnectionStringBuilder); } catch (Exception ex) { Invoke((MethodInvoker) delegate() { ApplyCursorToControlTree(Cursors.Default); }); GUI.BitscoreForms.BitscoreMessageBox.Show(this, "An error occurred while parsing the server instance selection.\n\n" + "Error details:\n\n" + ex.Message + "\n\n" + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Close(); return; } Close(); }); } catch (SqlException) { try { Invoke((MethodInvoker) delegate() { ApplyCursorToControlTree(Cursors.Default); }); MattimonAgentApplication.GUI.BitscoreForms.BitscoreMessageBox.Show("Unable to connect to SQL Server.\nMake sure that the provided credentials are valid and try again.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch { } } catch (Exception) { try { Invoke((MethodInvoker) delegate() { ApplyCursorToControlTree(Cursors.Default); }); MattimonAgentApplication.GUI.BitscoreForms.BitscoreMessageBox.Show("Unable to connect to SQL Server.\nMake sure that the provided credentials are valid and try again.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch { } } finally { try { Invoke((MethodInvoker) delegate() { ApplyCursorToControlTree(Cursors.Default); }); Invoke((MethodInvoker) delegate() { pnlSqlCred.Enabled = true; }); Invoke((MethodInvoker) delegate() { btnTestConnection.Enabled = true; }); Invoke((MethodInvoker) delegate() { btnAccept.Enabled = true; }); } catch { } } } }).Start(); }