/// <summary>Executes the INSERT command. Automatically generates the code that retrieves the new identity for
        /// the supported databases. DBMS specific code depends on the DBMS property of the used ConnectionProvider.</summary>
        /// <param name="insert">INSERT statement to execute.</param>
        /// <param name="dbms">Target DBMS.</param>
        /// <param name="conn">Connection-transaction context to use.</param>
        /// <param name="lastExecutedCommandInfo">Output parameter: statistic for executed command.</param>
        /// <param name="cmdTimeout">Timeout for this procedure</param>
        /// <returns>Automatically generated ID for inserted row, or <b>null</b> if ID is not automatically generated.</returns>
        public object Execute(InsertStatement insert, DbmsType dbms, IConnectionProvider conn, out CommandExecutionStatistics lastExecutedCommandInfo, int cmdTimeout = 30)
        {
            // Renderer and DBMS will compute next ID, insert row and retrieve ID in one trip.
            StringBuilder cmdtxt = new StringBuilder();
            DbParameterCollection parameters = new DbParameterCollection();
            DbParameter newId = RenderInsert(insert, null, dbms, cmdtxt, parameters);
            string command = cmdtxt.ToString();

            lastExecutedCommandInfo = new CommandExecutionStatistics(command);
            DbUtil.ExecuteNonQuery(conn, command, parameters, CommandType.Text, cmdTimeout);
            lastExecutedCommandInfo.StopTime();

            object id = null;
            if (newId != null)
                id = Convert.ToInt32(newId.Value, CultureInfo.InvariantCulture);

            return id;
        }
Example #2
0
        /// <summary>Executes the INSERT command. Automatically generates the code that retrieves the new identity for
        /// the supported databases. DBMS specific code depends on the DBMS property of the used ConnectionProvider.</summary>
        /// <param name="insert">INSERT statement to execute.</param>
        /// <param name="dbms">Target DBMS.</param>
        /// <param name="conn">Connection-transaction context to use.</param>
        /// <param name="lastExecutedCommandInfo">Output parameter: statistic for executed command.</param>
        /// <param name="cmdTimeout">Timeout for the execution.</param>
        /// <returns>Automatically generated ID for inserted row, or <b>null</b> if ID is not automatically generated.</returns>
        public object Execute(InsertStatement insert, DbmsType dbms, IConnectionProvider conn, out CommandExecutionStatistics lastExecutedCommandInfo, int cmdTimeout = 30)
        {
            // Renderer and DBMS will compute next ID, insert row and retrieve ID in one trip.
            StringBuilder cmdtxt = new StringBuilder();
            DbParameterCollection parameters = new DbParameterCollection();
            RenderInsert(insert, null, dbms, cmdtxt, parameters);
            string command = cmdtxt.ToString();

            lastExecutedCommandInfo = new CommandExecutionStatistics(command);
            object id = null;
            if (HasAutoIdField(insert.Table))
                id = InsertAndSelectAutoNumber(conn, command, parameters, cmdTimeout);
            else
                Insert(conn, command, parameters, cmdTimeout);

            lastExecutedCommandInfo.StopTime();
            return id;
        }
        /// <summary>Executes the INSERT command. Automatically generates the code that retrieves the new identity for
        /// the supported databases. DBMS specific code depends on the DBMS property of the used ConnectionProvider.</summary>
        /// <param name="insert">INSERT statement to execute.</param>
        /// <param name="dbms">Target DBMS.</param>
        /// <param name="conn">Connection-transaction context to use.</param>
        /// <param name="lastExecutedCommandInfo">Output parameter: statistic for executed command.</param>
        /// <param name="cmdTimeout">Timeout for the execution.</param>
        /// <returns>Automatically generated ID for inserted row, or <b>null</b> if ID is not automatically generated.</returns>
        public object Execute(InsertStatement insert, DbmsType dbms, IConnectionProvider conn, out CommandExecutionStatistics lastExecutedCommandInfo, int cmdTimeout = 30)
        {
            // Renderer and DBMS will compute next ID, insert row and retrieve ID in one trip.
            StringBuilder cmdtxt = new StringBuilder();
            DbParameterCollection parameters = new DbParameterCollection();
            RenderInsert(insert, null, dbms, cmdtxt, parameters);

            string command = cmdtxt.ToString();
            object id;
            lastExecutedCommandInfo = new CommandExecutionStatistics(command);
            if (HasAutoIdField(insert.Table))
            {
                DataTable autoIncrementData = DbUtil.ExecuteQuery(conn, command, parameters, CommandType.Text, null, cmdTimeout);
                id = autoIncrementData.Rows[0][0];
                if (id == DBNull.Value)
                    id = null;
            }
            else
            {
                DbUtil.ExecuteNonQuery(conn, command, parameters, CommandType.Text, cmdTimeout);
                id = null;
            }

            lastExecutedCommandInfo.StopTime();
            return id;
        }