示例#1
0
        /// <summary>
        /// Executa o salvamento propriamente dito.
        /// </summary>
        public void Execute()
        {
            sum.Reset();

            try
            {
                if (dbConn != null)
                {
                    CommitChanges();
                }
                else
                {
                    using (dbConn = conn.CreateConnection())
                    {
                        dbConn.Open();
                        using (trans = dbConn.BeginTransaction(IsolationLevel.Serializable))
                            CommitChanges();
                    }
                }
            }
            catch (ConnectorException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw ConnectorExceptionFactory.FromDatabaseException(ex);
            }
        }
        protected override void CommitChanges()
        {
            try
            {
                IEnumerator en      = rows.GetEnumerator();
                bool        hasNext = en.MoveNext();
                while (hasNext)
                {
                    DataRow row = (DataRow)en.Current;

                    DataTable dt = row.Table;
                    using (new PhysicalTableAdapter(conn, dt))
                        using (AdpDataAdapter da = new AdpDataAdapter())
                        {
                            if (!conn.BuildCommands(da, dbConn, dt))
                            {
                                new AdpCommandBuilder(da, dbConn, dt);
                            }

                            if (trans != null)
                            {
                                SetTransaction(da);
                            }

                            do
                            {
                                if (row.RowState == DataRowState.Added)
                                {
                                    CommitInserts(dt, da, row);                             // HACK: otimizar para enviar todas a serem inseridas ao mesmo tempo
                                }
                                else
                                {
                                    da.Update(row);
                                }

                                // repete enquanto houverem mais linhas da mesma tabela
                                row = (hasNext = en.MoveNext()) ? (DataRow)en.Current : null;
                            } while (hasNext && Object.ReferenceEquals(row.Table, dt));
                        }
                }
            }
            catch (Exception ex)
            {
                if (ex is ConnectorException)
                {
                    throw;
                }
                else
                {
                    throw ConnectorExceptionFactory.FromDatabaseException(ex);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Salva no banco de dados as alterações em uma tabela.
        /// </summary>
        /// <param name="dt">A tabela</param>
        /// <param name="state">
        /// O estado que estará sendo processado. Pode ser
        /// <see cref="DataViewRowState.Added"/>,
        /// <see cref="DataViewRowState.ModifiedCurrent"/> ou
        /// <see cref="DataViewRowState.Deleted"/>
        /// </param>
        /// <returns>O número de registros atualizados</returns>
        protected int CommitTable(DataTable dt, DataViewRowState state)
        {
            int c = 0;

            if (dt != null && dt.Rows.Count > 0)
            {
                try
                {
                    using (new PhysicalTableAdapter(conn, dt))
                        using (AdpDataAdapter da = new AdpDataAdapter())
                        {
                            DataRow[] rows = dt.Select(null, null, state);

                            if (rows.Length > 0)
                            {
                                if (!conn.BuildCommands(da, dbConn, dt))
                                {
                                    new AdpCommandBuilder(da, dbConn, dt);
                                }

                                if (trans != null)
                                {
                                    SetTransaction(da);
                                }

                                if (state == DataViewRowState.Added)
                                {
                                    c += CommitInserts(dt, da, rows);
                                }
                                else
                                {
                                    c += da.Update(rows);
                                }
                            }
                        }
                }
                catch (Exception ex)
                {
                    throw ConnectorExceptionFactory.FromDatabaseException(ex).Detail("Error while updating data table named {0}", dt.TableName);
                }
            }
            return(c);
        }