System.Data.DataTable.GetChanges C# (CSharp) Method

GetChanges() public method

public GetChanges ( ) : DataTable
return DataTable
        public DataTable GetChanges()
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataTable.GetChanges|API> {0}", ObjectID);
            try
            {
                DataTable dtChanges = Clone();
                DataRow row = null;

                for (int i = 0; i < Rows.Count; i++)
                {
                    row = Rows[i];
                    if (row._oldRecord != row._newRecord)
                    {
                        dtChanges.ImportRow(row);
                    }
                }

                if (dtChanges.Rows.Count == 0)
                {
                    return null;
                }

                return dtChanges;
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }

Same methods

DataTable::GetChanges ( DataRowState rowStates ) : DataTable

Usage Example

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="dbTableName"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<SqlCommandObject> GeneratorCommand(string connectionName, string dbTableName, DataTable table, DataRowState state)
        {
            if (connectionName.IsEmpty())
                throw new Exception("GeneratorCommand方法中的connectionName为空.");

            List<SqlCommandObject> cmds = new List<SqlCommandObject>();
            if (table.IsEmpty())
                return cmds;

            InsertCommandGenerator insertCommandGenerator = new InsertCommandGenerator();
            DeleteCommandGenerator deleteCommandGenerator = new DeleteCommandGenerator();
            UpdateCommandGenerator updateCommandGenerator = new UpdateCommandGenerator();

            SqlCommandObject cmd = null;
            if (table != null)
            {
                DataTable dtChanges = null;
                if (state == DataRowState.Unchanged)
                {
                    dtChanges = table.GetChanges();
                }
                else
                {
                    dtChanges = table.GetChanges(state);
                }

                if (dtChanges == null) return cmds;

                if (dbTableName.IsEmpty())
                    throw new Exception("GeneratorCommand方法中的tableName为空.");

                foreach (DataRow dr in dtChanges.Rows)
                {
                    switch (dr.RowState)
                    {
                        case DataRowState.Deleted:
                            cmd = deleteCommandGenerator.GenerateCommand(connectionName, dbTableName, dr);
                            break;
                        case DataRowState.Modified:
                            cmd = updateCommandGenerator.GenerateCommand(connectionName, dbTableName, dr);
                            break;
                        case DataRowState.Added:
                            cmd = insertCommandGenerator.GenerateCommand(connectionName, dbTableName, dr);
                            break;
                        default:
                            cmd = null;
                            break;
                    }
                    if (cmd != null)
                    {
                        cmds.Add(cmd);
                    }
                }
            }
            return cmds;
        }
All Usage Examples Of System.Data.DataTable::GetChanges
DataTable