示例#1
0
 public void UpdateExecuteState(frmConnection oForm)
 {
     if (oForm == this.ActiveMdiChild)
     {
         tbbExecute.Enabled       = !oForm.IsExecuting;
         tbbCancelExecute.Enabled = oForm.IsExecuting;
     }
 }
示例#2
0
        private void Application_Idle(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild is frmConnection)
            {
                frmConnection oForm = (frmConnection)this.ActiveMdiChild;

                mnuQueryExecute.Enabled = !oForm.IsExecuting;
                mnuQueryCancel.Enabled  = oForm.IsExecuting;

                tbbNew.Enabled  = true;
                tbbOpen.Enabled = true;
                tbbSave.Enabled = true;

                tbbCut.Enabled  = oForm.HasSelection;
                tbbCopy.Enabled = oForm.HasSelection;

                IDataObject oData = null;
                try
                {
                    oData = Clipboard.GetDataObject();
                }
                catch
                {
                    // Do nothing.
                }
                if (oData == null)
                {
                    tbbPaste.Enabled = false;
                }
                else
                {
                    tbbPaste.Enabled = oData.GetDataPresent(DataFormats.Text);
                }

                tbbFind.Enabled          = true;
                tbbExecute.Enabled       = !oForm.IsExecuting;
                tbbCancelExecute.Enabled = oForm.IsExecuting;

                cboDatabases.Enabled = true;
            }
            else
            {
                mnuQueryExecute.Enabled = false;
                mnuQueryCancel.Enabled  = false;

                tbbNew.Enabled           = true;
                tbbOpen.Enabled          = true;
                tbbSave.Enabled          = false;
                tbbCut.Enabled           = false;
                tbbCopy.Enabled          = false;
                tbbPaste.Enabled         = false;
                tbbFind.Enabled          = false;
                tbbExecute.Enabled       = false;
                tbbCancelExecute.Enabled = false;

                cboDatabases.Enabled = false;
            }
        }
示例#3
0
        /*static public void MaximizeMdiChild(System.Windows.Forms.Form oForm)
         * {
         *      if (oForm.MdiParent != null)
         *              oForm.Bounds = GetClientRectangle(oForm.MdiParent);
         * }
         *
         * static public Rectangle GetClientRectangle(System.Windows.Forms.Form oForm)
         * {
         *      Rectangle rc = oForm.ClientRectangle;
         *      rc.Width -= SystemInformation.FrameBorderSize.Width;
         *      rc.Height -= SystemInformation.FrameBorderSize.Height;
         *      foreach (Control oControl in oForm.Controls)
         *      {
         *              if (oControl is ToolBar || oControl is StatusBar)
         *                      rc.Height -= oControl.Height;
         *      }
         *      return rc;
         * }*/

        static public bool OpenConnection(Form oParent, string sUsername, string sServer,
                                          string sConnectionString, bool fAutoStart, string sDatabaseName)
        {
            SqlConnection oConn = new SqlConnection(sConnectionString);

            if (fAutoStart)
            {
                string sServiceName = "MSSQLSERVER";
                string sMachineName = sServer;
                try
                {
                    int ichSlash = sMachineName.IndexOf("\\");
                    if (ichSlash != -1)
                    {
                        sServiceName = "MSSQL$" + sMachineName.Substring(ichSlash + 1);
                        sMachineName = sMachineName.Substring(0, ichSlash);
                    }

                    ServiceController oController = new ServiceController(sServiceName, sMachineName);
                    if (oController.Status == ServiceControllerStatus.Stopped)
                    {
                        oController.Start();
                        try
                        {
                            oController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
                            System.Threading.Thread.Sleep(2000);                             // Wait 2 seconds.
                        }
                        catch (System.ServiceProcess.TimeoutException oEx)
                        {
                            MessageBox.Show(oParent,
                                            "A timeout occurred while waiting for SQL Server on " + sMachineName + " to start.",
                                            Globals.ksAppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            string s = oEx.ToString();
                        }
                    }
                }
                catch (Exception oEx)
                {
                    MessageBox.Show(oParent,
                                    "Unable to retrieve the status of SQL Server on " + sMachineName + ":\n\n" +
                                    oEx.Message, Globals.ksAppTitle,
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
            }

            try
            {
                oConn.Open();

                if (oConn.Database.ToLower() != sDatabaseName.ToLower())
                {
                    try
                    {
                        oConn.ChangeDatabase(sDatabaseName);
                    }
                    catch (Exception oEx)
                    {
                        MessageBox.Show(oParent,
                                        "Unable to connect to database " + sDatabaseName + ". The current database is " + oConn.Database + ".",
                                        Globals.ksAppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                frmConnection oForm = new frmConnection(oConn, sUsername);
                oForm.MdiParent = Globals.MainForm;
                oForm.Show();

                return(true);
            }
            catch (Exception oEx)
            {
                MessageBox.Show(oParent,
                                "Unable to connect to server " + sServer + ":\n\n" +
                                oEx.Message, Globals.ksAppTitle,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }
        }
示例#4
0
        /*static public void MaximizeMdiChild(System.Windows.Forms.Form oForm)
        {
            if (oForm.MdiParent != null)
                oForm.Bounds = GetClientRectangle(oForm.MdiParent);
        }

        static public Rectangle GetClientRectangle(System.Windows.Forms.Form oForm)
        {
            Rectangle rc = oForm.ClientRectangle;
            rc.Width -= SystemInformation.FrameBorderSize.Width;
            rc.Height -= SystemInformation.FrameBorderSize.Height;
            foreach (Control oControl in oForm.Controls)
            {
                if (oControl is ToolBar || oControl is StatusBar)
                    rc.Height -= oControl.Height;
            }
            return rc;
        }*/
        public static bool OpenConnection(Form oParent, string sUsername, string sServer,
			string sConnectionString, bool fAutoStart, string sDatabaseName)
        {
            SqlConnection oConn = new SqlConnection(sConnectionString);

            if (fAutoStart)
            {
                string sServiceName = "MSSQLSERVER";
                string sMachineName = sServer;
                try
                {
                    int ichSlash = sMachineName.IndexOf("\\");
                    if (ichSlash != -1)
                    {
                        sServiceName = "MSSQL$" + sMachineName.Substring(ichSlash + 1);
                        sMachineName = sMachineName.Substring(0, ichSlash);
                    }

                    ServiceController oController = new ServiceController(sServiceName, sMachineName);
                    if (oController.Status == ServiceControllerStatus.Stopped)
                    {
                        oController.Start();
                        try
                        {
                            oController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
                            System.Threading.Thread.Sleep(2000); // Wait 2 seconds.
                        }
                        catch (System.ServiceProcess.TimeoutException oEx)
                        {
                            MessageBox.Show(oParent,
                                "A timeout occurred while waiting for SQL Server on " + sMachineName + " to start.",
                                Globals.ksAppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            string s = oEx.ToString();
                        }
                    }
                }
                catch (Exception oEx)
                {
                    MessageBox.Show(oParent,
                        "Unable to retrieve the status of SQL Server on " + sMachineName + ":\n\n" +
                        oEx.Message, Globals.ksAppTitle,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
            }

            try
            {
                oConn.Open();

                if (oConn.Database.ToLower() != sDatabaseName.ToLower())
                {
                    try
                    {
                        oConn.ChangeDatabase(sDatabaseName);
                    }
                    catch (Exception oEx)
                    {
                        MessageBox.Show(oParent,
                            "Unable to connect to database " + sDatabaseName + ". The current database is " + oConn.Database + ".",
                            Globals.ksAppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                frmConnection oForm = new frmConnection(oConn, sUsername);
                oForm.MdiParent = Globals.MainForm;
                oForm.Show();

                return true;
            }
            catch (Exception oEx)
            {
                MessageBox.Show(oParent,
                    "Unable to connect to server " + sServer + ":\n\n" +
                    oEx.Message, Globals.ksAppTitle,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
        }
示例#5
0
 public void UpdateExecuteState(frmConnection oForm)
 {
     if (oForm == this.ActiveMdiChild)
     {
         tbbExecute.Enabled = !oForm.IsExecuting;
         tbbCancelExecute.Enabled = oForm.IsExecuting;
     }
 }