Пример #1
0
 private void UpdateInternal(SqlDataAdapter dataAdapter, DataViewRowState recordStates, DataTable sourceTable, UpdateErrorHandler errorHandler)
 {
     try {
         while (true)
         {
             DataRow[]        changedRows = sourceTable.Select(null, null, recordStates);
             ErrorHandlerArgs args        = null;
             try {
                 SqlRowUpdatedEventHandler rowUpdated = delegate(object sender, SqlRowUpdatedEventArgs e) {
                     args = new ErrorHandlerArgs(sourceTable.TableName, e.Row, recordStates, e.Errors);
                 };
                 dataAdapter.RowUpdated += rowUpdated;
                 dataAdapter.Update(changedRows);
                 break;
             } catch {
                 if (errorHandler == null)
                 {
                     throw;
                 }
                 errorHandler(args);
                 if (args.Action == ErrorHandlingAction.ThrowError)
                 {
                     throw;
                 }
             }
         }
     } catch {
         throw;
     }
 }
Пример #2
0
        /// <summary>
        /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// sqlrowupdatedeventhandler.BeginInvoke(sender, e, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginInvoke(this SqlRowUpdatedEventHandler sqlrowupdatedeventhandler, Object sender, SqlRowUpdatedEventArgs e, AsyncCallback callback)
        {
            if (sqlrowupdatedeventhandler == null)
            {
                throw new ArgumentNullException("sqlrowupdatedeventhandler");
            }

            return(sqlrowupdatedeventhandler.BeginInvoke(sender, e, callback, null));
        }
Пример #3
0
        /// <include file='doc\SqlDataAdapter.uex' path='docs/doc[@for="SqlDataAdapter.OnRowUpdated1"]/*' />
        override protected void OnRowUpdated(RowUpdatedEventArgs value)
        {
            SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler)Events[ADP.EventRowUpdated];

            if ((null != handler) && (value is SqlRowUpdatedEventArgs))
            {
                handler(this, (SqlRowUpdatedEventArgs)value);
            }
        }
        protected override void OnRowUpdated(RowUpdatedEventArgs value)
        {
            SqlRowUpdatedEventHandler handler = (SqlRowUpdatedEventHandler)base.Events[EventRowUpdated];

            if ((handler != null) && (value is SqlRowUpdatedEventArgs))
            {
                handler(this, (SqlRowUpdatedEventArgs)value);
            }
            base.OnRowUpdated(value);
        }
        /// <summary>
        /// Handles saving the collection's items to the data source.
        /// </summary>
        ///
        /// <remarks>
        /// The base implementation of this method uses a DataAdapter with the collection's associated commands
        /// in order to save the contained records to the database. Derived classes can override this method to add additional
        /// checks or operations before and after the save, or to change the saving method altogether.
        /// </remarks>
        protected virtual void OnSave()
        {
            // Don't perform the save if there are no changes
            if (_table.GetChanges() == null)
            {
                return;
            }

            if (!DataManager.ProxyMode)
            {
                // Create an adapter
                SqlDataAdapter adapter = DataManager.CreateAdapter(this);

                // Vars for proxy operations
                DataTable updatedRowsTable = null;
                SqlRowUpdatedEventHandler updatedHander = null;

                if (ProxyServer.InProgress)
                {
                    updatedRowsTable = _table.Clone();
                    updatedHander    = delegate(object sender, SqlRowUpdatedEventArgs e)
                    {
                        if (e.StatementType != StatementType.Insert && e.StatementType != StatementType.Update)
                        {
                            return;
                        }

                        // Store and row that has been inserted or updated
                        updatedRowsTable.ImportRow(e.Row);
                    };

                    adapter.RowUpdated += updatedHander;
                }

                ConnectionKey ckey = null;
                using (adapter)
                {
                    try
                    {
                        ckey = DataManager.Current.OpenConnection(this);
                        adapter.Update(_table);
                    }
                    finally
                    {
                        DataManager.Current.CloseConnection(ckey);
                    }
                }

                // Wrap up the proxy operation
                if (ProxyServer.InProgress)
                {
                    adapter.RowUpdated -= updatedHander;
                    ProxyServer.Current.Result[ProxyServer.Current.CurrentAction].AddData("updated", updatedRowsTable);
                }
            }
            else
            {
                ProxyRequestAction action = ProxyClient.Request.AddAction(this, MethodInfo.GetCurrentMethod());
                action.OnComplete = delegate()
                {
                    DataTable updated      = ProxyClient.Result[action].GetData <DataTable>("updated");
                    bool      usingInnerID = updated.Columns.Contains(Const.InnerIDColumn);
                    if (!usingInnerID)
                    {
                        throw new Exception("not using inner ID!!!");
                    }

                    // Go over each row and find matching rows
                    for (int i = 0; i < updated.Rows.Count; i++)
                    {
                        bool    merged     = false;
                        DataRow updatedRow = updated.Rows[i];
                        object  testID     = usingInnerID ? updatedRow[Const.InnerIDColumn] : null;
                        if (testID is int)
                        {
                            int       innerID = (int)testID;
                            DataRow[] rs      = _table.Select(String.Format("{0} = {1}", Const.InnerIDColumn, innerID));
                            if (rs.Length > 0)
                            {
                                // If a row with a matching innerID is found, merge the values and accept changes
                                DataRow row = rs[0];
                                row.ItemArray = updatedRow.ItemArray;
                                row.AcceptChanges();
                                merged = true;
                            }
                        }

                        // TODO: make sure we shouldn't be clearing the table before adding unidentified rows
                        if (!merged)
                        {
                            // Insert the new row at the same index
                            DataRow newRow = _table.NewRow();
                            newRow.ItemArray = updatedRow.ItemArray;
                            _table.Rows.InsertAt(newRow, i);

                            DataItem newItem = NewItem(newRow);
                            _innerList.Insert(i, newItem);

                            if (_hash != null)
                            {
                                _hash.Add(GetPrimaryKeyValue(newItem), newItem);
                            }
                        }
                    }
                };
            }
        }