Esempio n. 1
0
        private int UpdateInsertedRows(guard_profiles_dbDataSet2 dataSet, List <DataRow> allAddedRows)
        {
            int result = 0;

            if (this._tbl_sg_profilesTableAdapter != null)
            {
                DataRow[] addedRows = dataSet.Tbl_sg_profiles.Select(null, null, DataViewRowState.Added);
                if (addedRows != null && 0 < (int)addedRows.Length)
                {
                    result += this._tbl_sg_profilesTableAdapter.Update(addedRows);
                    allAddedRows.AddRange(addedRows);
                }
            }
            return(result);
        }
Esempio n. 2
0
        private int UpdateUpdatedRows(guard_profiles_dbDataSet2 dataSet, List <DataRow> allChangedRows, List <DataRow> allAddedRows)
        {
            int result = 0;

            if (this._tbl_sg_profilesTableAdapter != null)
            {
                DataRow[] updatedRows = dataSet.Tbl_sg_profiles.Select(null, null, DataViewRowState.ModifiedCurrent);
                updatedRows = this.GetRealUpdatedRows(updatedRows, allAddedRows);
                if (updatedRows != null && 0 < (int)updatedRows.Length)
                {
                    result += this._tbl_sg_profilesTableAdapter.Update(updatedRows);
                    allChangedRows.AddRange(updatedRows);
                }
            }
            return(result);
        }
 public virtual int Update(guard_profiles_dbDataSet2 dataSet)
 {
     return(this.Adapter.Update(dataSet, "Tbl_sg_profiles"));
 }
Esempio n. 4
0
        public virtual int UpdateAll(guard_profiles_dbDataSet2 dataSet)
        {
            if (dataSet == null)
            {
                throw new ArgumentNullException("dataSet");
            }
            if (!dataSet.HasChanges())
            {
                return(0);
            }
            if (this._tbl_sg_profilesTableAdapter != null && !this.MatchTableAdapterConnection(this._tbl_sg_profilesTableAdapter.Connection))
            {
                throw new ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection string.");
            }
            IDbConnection workConnection = this.Connection;

            if (workConnection == null)
            {
                throw new ApplicationException("TableAdapterManager contains no connection information. Set each TableAdapterManager TableAdapter property to a valid TableAdapter instance.");
            }
            bool workConnOpened = false;

            if ((workConnection.State & ConnectionState.Broken) == ConnectionState.Broken)
            {
                workConnection.Close();
            }
            if (workConnection.State == ConnectionState.Closed)
            {
                workConnection.Open();
                workConnOpened = true;
            }
            IDbTransaction workTransaction = workConnection.BeginTransaction();

            if (workTransaction == null)
            {
                throw new ApplicationException("The transaction cannot begin. The current data connection does not support transactions or the current state is not allowing the transaction to begin.");
            }
            List <DataRow>     allChangedRows = new List <DataRow>();
            List <DataRow>     allAddedRows   = new List <DataRow>();
            List <DataAdapter> adaptersWithAcceptChangesDuringUpdate = new List <DataAdapter>();
            Dictionary <object, IDbConnection> revertConnections     = new Dictionary <object, IDbConnection>();
            int     result        = 0;
            DataSet backupDataSet = null;

            if (this.BackupDataSetBeforeUpdate)
            {
                backupDataSet = new DataSet();
                backupDataSet.Merge(dataSet);
            }
            try
            {
                try
                {
                    if (this._tbl_sg_profilesTableAdapter != null)
                    {
                        revertConnections.Add(this._tbl_sg_profilesTableAdapter, this._tbl_sg_profilesTableAdapter.Connection);
                        this._tbl_sg_profilesTableAdapter.Connection  = (SqlConnection)workConnection;
                        this._tbl_sg_profilesTableAdapter.Transaction = (SqlTransaction)workTransaction;
                        if (this._tbl_sg_profilesTableAdapter.Adapter.AcceptChangesDuringUpdate)
                        {
                            this._tbl_sg_profilesTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                            adaptersWithAcceptChangesDuringUpdate.Add(this._tbl_sg_profilesTableAdapter.Adapter);
                        }
                    }
                    if (this.UpdateOrder != TableAdapterManager.UpdateOrderOption.UpdateInsertDelete)
                    {
                        result += this.UpdateInsertedRows(dataSet, allAddedRows);
                        result += this.UpdateUpdatedRows(dataSet, allChangedRows, allAddedRows);
                    }
                    else
                    {
                        result += this.UpdateUpdatedRows(dataSet, allChangedRows, allAddedRows);
                        result += this.UpdateInsertedRows(dataSet, allAddedRows);
                    }
                    result += this.UpdateDeletedRows(dataSet, allChangedRows);
                    workTransaction.Commit();
                    if (0 < allAddedRows.Count)
                    {
                        DataRow[] rows = new DataRow[allAddedRows.Count];
                        allAddedRows.CopyTo(rows);
                        for (int i = 0; i < (int)rows.Length; i++)
                        {
                            rows[i].AcceptChanges();
                        }
                    }
                    if (0 < allChangedRows.Count)
                    {
                        DataRow[] rows = new DataRow[allChangedRows.Count];
                        allChangedRows.CopyTo(rows);
                        for (int i = 0; i < (int)rows.Length; i++)
                        {
                            rows[i].AcceptChanges();
                        }
                    }
                }
                catch (Exception exception)
                {
                    Exception ex = exception;
                    workTransaction.Rollback();
                    if (this.BackupDataSetBeforeUpdate)
                    {
                        dataSet.Clear();
                        dataSet.Merge(backupDataSet);
                    }
                    else if (0 < allAddedRows.Count)
                    {
                        DataRow[] rows = new DataRow[allAddedRows.Count];
                        allAddedRows.CopyTo(rows);
                        for (int i = 0; i < (int)rows.Length; i++)
                        {
                            DataRow row = rows[i];
                            row.AcceptChanges();
                            row.SetAdded();
                        }
                    }
                    throw ex;
                }
            }
            finally
            {
                if (workConnOpened)
                {
                    workConnection.Close();
                }
                if (this._tbl_sg_profilesTableAdapter != null)
                {
                    this._tbl_sg_profilesTableAdapter.Connection  = (SqlConnection)revertConnections[this._tbl_sg_profilesTableAdapter];
                    this._tbl_sg_profilesTableAdapter.Transaction = null;
                }
                if (0 < adaptersWithAcceptChangesDuringUpdate.Count)
                {
                    DataAdapter[] adapters = new DataAdapter[adaptersWithAcceptChangesDuringUpdate.Count];
                    adaptersWithAcceptChangesDuringUpdate.CopyTo(adapters);
                    for (int i = 0; i < (int)adapters.Length; i++)
                    {
                        adapters[i].AcceptChangesDuringUpdate = true;
                    }
                }
            }
            return(result);
        }