/// <summary>
        /// Execute the load operation
        /// </summary>
        /// <returns>The number of rows inserted.</returns>
        public int Load()
        {
            bool openedConnection = false;

            if (Connection == null)
            {
                throw new InvalidOperationException(Resources.ConnectionNotSet);
            }

            // next we open up the connetion if it is not already open
            if (connection.State != ConnectionState.Open)
            {
                openedConnection = true;
                connection.Open();
            }

            try
            {
                string       sql = BuildSqlCommand();
                MyCatCommand cmd = new MyCatCommand(sql, Connection);
                cmd.CommandTimeout = Timeout;
                return(cmd.ExecuteNonQuery());
            }
            finally
            {
                if (openedConnection)
                {
                    connection.Close();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Updates the given table with data from the given <see cref="DataSet"/>
        /// </summary>
        /// <param name="connectionString">Settings to use for the update</param>
        /// <param name="commandText">Command text to use for the update</param>
        /// <param name="ds"><see cref="DataSet"/> containing the new data to use in the update</param>
        /// <param name="tablename">Tablename in the dataset to update</param>
        public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename)
        {
            MyCatConnection cn = new MyCatConnection(connectionString);

            cn.Open();
            MyCatDataAdapter    da = new MyCatDataAdapter(commandText, cn);
            MyCatCommandBuilder cb = new MyCatCommandBuilder(da);

            cb.ToString();
            da.Update(ds, tablename);
            cn.Close();
        }
        public void Close()
#endif
        {
            if (!isOpen)
            {
                return;
            }

            bool            shouldCloseConnection = (commandBehavior & CommandBehavior.CloseConnection) != 0;
            CommandBehavior originalBehavior      = commandBehavior;

            // clear all remaining resultsets
            try
            {
                // Temporarily change to Default behavior to allow NextResult to finish properly.
                if (!originalBehavior.Equals(CommandBehavior.SchemaOnly))
                {
                    commandBehavior = CommandBehavior.Default;
                }
                while (NextResult())
                {
                }
            }
            catch (MyCatException ex)
            {
                // Ignore aborted queries
                if (!ex.IsQueryAborted)
                {
                    // ignore IO exceptions.
                    // We are closing or disposing reader, and  do not
                    // want exception to be propagated to used. If socket is
                    // is closed on the server side, next query will run into
                    // IO exception. If reader is closed by GC, we also would
                    // like to avoid any exception here.
                    bool isIOException = false;
                    for (Exception exception = ex; exception != null;
                         exception = exception.InnerException)
                    {
                        if (exception is System.IO.IOException)
                        {
                            isIOException = true;
                            break;
                        }
                    }
                    if (!isIOException)
                    {
                        // Ordinary exception (neither IO nor query aborted)
                        throw;
                    }
                }
            }
            catch (System.IO.IOException)
            {
                // eat, on the same reason we eat IO exceptions wrapped into
                // MyCatExceptions reasons, described above.
            }
            finally
            {
                // always ensure internal reader is null (Bug #55558)
                connection.Reader = null;
                commandBehavior   = originalBehavior;
            }
            // we now give the command a chance to terminate.  In the case of
            // stored procedures it needs to update out and inout parameters
            command.Close(this);
            commandBehavior = CommandBehavior.Default;

            if (this.command.Canceled && connection.driver.Version.isAtLeast(5, 1, 0))
            {
                // Issue dummy command to clear kill flag
                ClearKillFlag();
            }

            if (shouldCloseConnection)
            {
                connection.Close();
            }

            command            = null;
            connection.IsInUse = false;
            connection         = null;
            isOpen             = false;
        }
        /// <summary>
        /// Executes this instance.
        /// </summary>
        /// <returns>The number of statements executed as part of the script.</returns>
        public int Execute()
        {
            bool openedConnection = false;

            if (connection == null)
            {
                throw new InvalidOperationException(Resources.ConnectionNotSet);
            }
            if (query == null || query.Length == 0)
            {
                return(0);
            }

            // next we open up the connetion if it is not already open
            if (connection.State != ConnectionState.Open)
            {
                openedConnection = true;
                connection.Open();
            }

            // since we don't allow setting of parameters on a script we can
            // therefore safely allow the use of user variables.  no one should be using
            // this connection while we are using it so we can temporarily tell it
            // to allow the use of user variables
            bool allowUserVars = connection.Settings.AllowUserVariables;

            connection.Settings.AllowUserVariables = true;

            try
            {
                string mode = connection.driver.Property("sql_mode");
                mode = StringUtility.ToUpperInvariant(mode);
                bool ansiQuotes         = mode.IndexOf("ANSI_QUOTES") != -1;
                bool noBackslashEscapes = mode.IndexOf("NO_BACKSLASH_ESCAPES") != -1;

                // first we break the query up into smaller queries
                List <ScriptStatement> statements = BreakIntoStatements(ansiQuotes, noBackslashEscapes);

                int          count = 0;
                MyCatCommand cmd   = new MyCatCommand(null, connection);
                foreach (ScriptStatement statement in statements)
                {
                    if (String.IsNullOrEmpty(statement.text))
                    {
                        continue;
                    }
                    cmd.CommandText = statement.text;
                    try
                    {
                        cmd.ExecuteNonQuery();
                        count++;
                        OnQueryExecuted(statement);
                    }
                    catch (Exception ex)
                    {
                        if (Error == null)
                        {
                            throw;
                        }
                        if (!OnScriptError(ex))
                        {
                            break;
                        }
                    }
                }
                OnScriptCompleted();
                return(count);
            }
            finally
            {
                connection.Settings.AllowUserVariables = allowUserVars;
                if (openedConnection)
                {
                    connection.Close();
                }
            }
        }