示例#1
0
        private string GetActiveDocumentContent()
        {
            if (_mainForm.ActiveDocument == null)
            {
                return(null);
            }

            SqlEditForm document = _mainForm.ActiveDocument as SqlEditForm;

            if (document == null)
            {
                return(null);
            }

            if (document.Editor.Selection.Length > 0)
            {
                return(document.Editor.Selection.Text);
            }
            else
            {
                return(document.Editor.Text);
            }
        }
示例#2
0
        private void RunQuery()
        {
            SqlEditForm activeDocument = GetActiveDocument();

            if (activeDocument == null)
            {
                return;
            }

            string dataSetName =
                Path.GetFileNameWithoutExtension(activeDocument.FileName);

            if (String.IsNullOrEmpty(dataSetName))
            {
                dataSetName = Resources.QueryDataSetName;
            }

            activeDocument.ClearData();
            _output.ClearOutputViews();

            _output.Text = Resources.OutputWindowRunQuery;
            _mainForm.SetStatusBarMessage(String.Empty);

            List <string> commands = GetCommands();

            if (commands == null)
            {
                return;
            }

            SqlConnection activeConnection =
                _sqlConnectionManager.SelectedConnection;

            if (activeConnection == null)
            {
                return;
            }

            _output.Text = String.Format("{0}: {1}",
                                         Resources.OutputWindowRunQuery,
                                         activeConnection.Name);

            DbConnection  cnn         = null;
            DbTransaction trx         = null;
            DbCommand     cmd         = null;
            DbDataAdapter dataAdapter = null;
            DataSet       dataSet     = new DataSet(dataSetName);

            WriteLog(String.Format("------ {0}: {1}",
                                   Resources.QueryStarted,
                                   activeDocument.FileName));

            _mainForm.SetStatusBarMessage(Resources.StatusBarQueryStarted);

            _mainForm.StatusBar.Refresh();

            string logMessage     = Resources.QueryUnknownCommand;
            bool   useTransaction = (commands.Count > 1);

            try
            {
                DbProviderFactory factory =
                    DbProviderFactories.GetFactory(
                        activeConnection.Provider.InvariantName);

                cnn = factory.CreateConnection();
                cnn.ConnectionString = activeConnection.ConnectionString;
                cnn.Open();

                cmd             = factory.CreateCommand();
                cmd.Connection  = cnn;
                cmd.CommandText = String.Empty;

                if (useTransaction)
                {
                    trx             = cnn.BeginTransaction();
                    cmd.Transaction = trx;

                    WriteLog(Resources.QueryBeginTransaction);
                }

                dataAdapter = factory.CreateDataAdapter();
                dataAdapter.SelectCommand = cmd;

                int commandCount = 1;

                foreach (string command in commands)
                {
                    cmd.CommandText = command;

                    string commandType = GetCommandName(command);

                    logMessage = commandType;

                    if (commandType == Resources.QuerySelectCommand)
                    {
                        int res = dataAdapter.Fill(dataSet,
                                                   String.Format(
                                                       Resources.QueryDataRowName,
                                                       commandCount++));

                        if (res < 0)
                        {
                            res = 0;
                        }

                        WriteLog(String.Format(
                                     Resources.QuerySelectSuccess,
                                     logMessage, res, res == 1 ?
                                     Resources.QueryRowSingular :
                                     Resources.QueryRowPlural));
                    }
                    else
                    {
                        int res = cmd.ExecuteNonQuery();

                        if (res < 0)
                        {
                            res = 0;
                        }

                        WriteLog(String.Format(
                                     Resources.QueryNonSelectSuccess,
                                     logMessage, res, res == 1 ?
                                     Resources.QueryRowSingular :
                                     Resources.QueryRowPlural));
                    }
                }

                if (useTransaction)
                {
                    trx.Commit();
                    WriteLog(Resources.QueryCommitTransaction);
                }

                _mainForm.SetStatusBarMessage(
                    Resources.StatusBarQuerySuccess);

                activeDocument.DataSet = dataSet;
                activeDocument.ShowTable();
            }
            catch (Exception ex)
            {
                string errorMessage =
                    ex.Message.Replace("\r\n", " ");

                WriteError(String.Format(
                               Resources.QueryErrors,
                               logMessage, errorMessage));

                _mainForm.SetStatusBarMessage(Resources.StatusBarQueryError);

                if (useTransaction && trx != null)
                {
                    trx.Rollback();
                    WriteLog(Resources.QueryRollbackTransaction);
                }
            }
            finally
            {
                if (cnn != null)
                {
                    cnn.Dispose();
                }
            }

            WriteLog(String.Format("------ {0}",
                                   Resources.QueryComplete));
        }