Пример #1
0
        /// <summary>
        /// Queries a generic sql command.
        /// </summary>
        /// <param name="cmd">SqlCommand to query</param>
        /// <returns>DataTable if successful, null if anything goes wrong</returns>
        public DataTable Query(SqlCommand cmd)
        {
            try
            {
                if (!BatchJob && Environment.UserInteractive)
                {
                    if (BeforeQueryAction != null)
                    {
                        BeforeQueryAction.Invoke();
                    }
                }

                OpenConnection();

                cmd.Connection     = _connection;
                cmd.CommandTimeout = Timeout;

                var dsh = new DataSet();

                if (ShowSql)
                {
                    Debug.WriteLine(cmd.CommandText);
                }

                new SqlDataAdapter(cmd).Fill(dsh);

                return(dsh.Tables.Count <= 0 ? null : dsh.Tables[0]);
            }
            catch (Exception exception)
            {
                if (!DontShowExceptions && Environment.UserInteractive)
                {
                    if (MessageProducedEvent != null)
                    {
                        var title = "SQL HATASI";

                        if (cmd.Connection != null)
                        {
                            title = " (" + cmd.Connection.DataSource + "; " + cmd.Connection.Database + ")";
                        }

                        MessageProducedEvent.Invoke(exception.Message, title);
                    }
                }

                return(null);
            }
            finally
            {
                if (!BatchJob)
                {
                    if (AfterQueryAction != null)
                    {
                        AfterQueryAction.Invoke();
                    }
                }

                CloseConnection();
            }
        }
Пример #2
0
        /// <summary>
        /// Executes a SqlCommand object.
        /// </summary>
        /// <param name="cmd">SqlCommand object to execute.</param>
        /// <returns>True if successful, false otherwise.</returns>
        public bool Execute(SqlCommand cmd)
        {
            try
            {
                if (!BatchJob && Environment.UserInteractive)
                {
                    if (BeforeQueryAction != null)
                    {
                        BeforeQueryAction.Invoke();
                    }
                }

                OpenConnection();

                cmd.Connection     = _connection;
                cmd.CommandTimeout = Timeout;

                if (ShowSql)
                {
                    Debug.WriteLine(cmd.CommandText);
                }

                cmd.ExecuteNonQuery();

                return(true);
            }
            catch (Exception exception)
            {
                if (!DontShowExceptions && Environment.UserInteractive)
                {
                    if (MessageProducedEvent != null)
                    {
                        var title = "SQL HATASI";

                        if (cmd.Connection != null)
                        {
                            title = " (" + cmd.Connection.DataSource + "; " + cmd.Connection.Database + ")";
                        }

                        MessageProducedEvent.Invoke(exception.Message, title);
                    }
                }

                LogHelper.Log("Exception: " + exception.Message);
                LogHelper.Log(exception.GetAllInnerExceptions().Select(exc => exc.Message).Concatenate("\n") + "\n" + exception.StackTrace);

                return(false);
            }
            finally
            {
                if (!BatchJob && Environment.UserInteractive)
                {
                    if (AfterQueryAction != null)
                    {
                        AfterQueryAction.Invoke();
                    }
                }

                CloseConnection();
            }
        }