当前位置: 首页>>代码示例>>C#>>正文


C# DataTable.RejectChanges方法代码示例

本文整理汇总了C#中DataTable.RejectChanges方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.RejectChanges方法的具体用法?C# DataTable.RejectChanges怎么用?C# DataTable.RejectChanges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataTable的用法示例。


在下文中一共展示了DataTable.RejectChanges方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: setAdded_testRollback

        public void setAdded_testRollback()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col1", typeof(int));
            table.Columns.Add("col2", typeof(int));

            table.Rows.Add(new object[] { 1, 1 });
            table.AcceptChanges();

            table.Rows[0].SetAdded();
            table.RejectChanges();
            Assert.Equal(0, table.Rows.Count);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:13,代码来源:DataRowTest2.cs

示例2: setModified_testRollback

        public void setModified_testRollback()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col1", typeof(int));
            table.Columns.Add("col2", typeof(int));

            DataRow row = table.Rows.Add(new object[] { 1, 1 });
            table.AcceptChanges();

            row.SetModified();
            Assert.Equal(row.RowState, DataRowState.Modified);
            Assert.Equal(1, row[0, DataRowVersion.Current]);
            Assert.Equal(1, row[0, DataRowVersion.Original]);
            table.RejectChanges();
            Assert.Equal(row.RowState, DataRowState.Unchanged);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:16,代码来源:DataRowTest2.cs

示例3: ImportRowDeletedTest

        public void ImportRowDeletedTest()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col", typeof(int));
            table.Columns.Add("col1", typeof(int));

            DataRow row = table.Rows.Add(new object[] { 1, 2 });
            table.PrimaryKey = new DataColumn[] { table.Columns[0] };
            table.AcceptChanges();

            // If row is in Deleted state, then ImportRow loads the
            // row.
            row.Delete();
            table.ImportRow(row);
            Assert.Equal(2, table.Rows.Count);

            // Both the deleted rows shud be now gone
            table.AcceptChanges();
            Assert.Equal(0, table.Rows.Count);

            //just add another row
            row = table.Rows.Add(new object[] { 1, 2 });
            // no exception shud be thrown
            table.AcceptChanges();

            // If row is in Deleted state, then ImportRow loads the
            // row and validate only on RejectChanges
            row.Delete();
            table.ImportRow(row);
            Assert.Equal(2, table.Rows.Count);
            Assert.Equal(DataRowState.Deleted, table.Rows[1].RowState);

            try
            {
                table.RejectChanges();
                Assert.False(true);
            }
            catch (ConstraintException ex)
            {
                // Column 'col' is constrained to be unique.
                // Value '1' is already present
                Assert.Equal(typeof(ConstraintException), ex.GetType());
                Assert.Null(ex.InnerException);
                Assert.NotNull(ex.Message);
                Assert.True(ex.Message.IndexOf("'col'") != -1);
                Assert.True(ex.Message.IndexOf("'1'") != -1);
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:48,代码来源:DataTableTest.cs

示例4: Changes

        public void Changes() //To test GetChanges and RejectChanges
        {
            DataTable table = new DataTable();

            DataColumn col = new DataColumn();
            col.ColumnName = "Id";
            col.DataType = typeof(int);
            table.Columns.Add(col);
            UniqueConstraint uc = new UniqueConstraint("UK1", table.Columns[0]);
            table.Constraints.Add(uc);

            col = new DataColumn();
            col.ColumnName = "Name";
            col.DataType = typeof(string);
            table.Columns.Add(col);

            DataRow row = table.NewRow();
            row["Id"] = 147;
            row["name"] = "Abc";
            table.Rows.Add(row);
            table.AcceptChanges();

            row = table.NewRow();
            row["Id"] = 47;
            row["name"] = "Efg";
            table.Rows.Add(row);

            //Testing GetChanges
            DataTable changesTable = table.GetChanges();
            Assert.Equal(1, changesTable.Rows.Count);
            Assert.Equal("Efg", changesTable.Rows[0]["Name"]);
            table.AcceptChanges();
            changesTable = table.GetChanges();

            try
            {
                int cnt = changesTable.Rows.Count;
                Assert.False(true);
            }
            catch (NullReferenceException)
            {
            }

            //Testing RejectChanges
            row = table.NewRow();
            row["Id"] = 247;
            row["name"] = "Hij";
            table.Rows.Add(row);

            (table.Rows[0])["Name"] = "AaBbCc";
            table.RejectChanges();
            Assert.Equal("Abc", (table.Rows[0])["Name"]);
            Assert.Equal(2, table.Rows.Count);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:54,代码来源:DataTableTest.cs

示例5: FindByKey

        public void FindByKey()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col1", typeof(int));
            table.PrimaryKey = new DataColumn[] { table.Columns[0] };

            table.Rows.Add(new object[] { 1 });
            table.Rows.Add(new object[] { 2 });
            table.Rows.Add(new object[] { 3 });
            table.AcceptChanges();

            Assert.NotNull(table.Rows.Find(new object[] { 1 }));

            table.Rows[0].Delete();
            Assert.Null(table.Rows.Find(new object[] { 1 }));

            table.RejectChanges();
            Assert.NotNull(table.Rows.Find(new object[] { 1 }));
        }
开发者ID:dotnet,项目名称:corefx,代码行数:19,代码来源:DataRowCollectionTest2.cs

示例6: LoadRowTestUpsert

        public void LoadRowTestUpsert()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("name", typeof(string));

            dt.Rows.Add(new object[] { 1, "mono 1" });
            dt.Rows.Add(new object[] { 2, "mono 2" });
            dt.Rows.Add(new object[] { 3, "mono 3" });

            dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };

            dt.AcceptChanges();
            try
            {
                SubscribeEvents(dt);

                ResetEventFlags();
                dt.LoadDataRow(new object[] { 2, "mono test" }, LoadOption.Upsert);
                Assert.Equal(3, dt.Rows.Count);
                Assert.Equal("mono test", dt.Rows[1][1]);
                Assert.Equal("mono 2", dt.Rows[1][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[1], _rowInAction_Changing);
                Assert.Equal(DataRowAction.Change, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[1], _rowInAction_Changed);
                Assert.Equal(DataRowAction.Change, _rowAction_Changed);


                // Row State tests
                // current - modified ; result - modified
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 2, "mono test 2" }, LoadOption.Upsert);
                Assert.Equal("mono test 2", dt.Rows[1][1]);
                Assert.Equal("mono 2", dt.Rows[1][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[1], _rowInAction_Changing);
                Assert.Equal(DataRowAction.Change, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[1], _rowInAction_Changed);
                Assert.Equal(DataRowAction.Change, _rowAction_Changed);


                // current - Unchanged; result - Unchanged if no new value
                dt.AcceptChanges();
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 2, "mono test 2" }, LoadOption.Upsert);
                Assert.Equal("mono test 2", dt.Rows[1][1]);
                Assert.Equal("mono test 2", dt.Rows[1][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[1], _rowInAction_Changing);
                Assert.Equal(DataRowAction.Nothing, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[1], _rowInAction_Changed);
                Assert.Equal(DataRowAction.Nothing, _rowAction_Changed);

                // not the same value again
                dt.RejectChanges();
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 2, "mono test 3" }, LoadOption.Upsert);
                Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[1], _rowInAction_Changing);
                Assert.Equal(DataRowAction.Change, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[1], _rowInAction_Changed);
                Assert.Equal(DataRowAction.Change, _rowAction_Changed);


                // current - added; result - added
                dt.Rows.Add(new object[] { 4, "mono 4" });
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 4, "mono 4" }, LoadOption.Upsert);
                Assert.Equal("mono 4", dt.Rows[3][1]);
                try
                {
                    object o = dt.Rows[3][1, DataRowVersion.Original];
                    Assert.False(true);
                }
                catch (VersionNotFoundException) { }
                Assert.Equal(DataRowState.Added, dt.Rows[3].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[3], _rowInAction_Changing);
                Assert.Equal(DataRowAction.Change, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[3], _rowInAction_Changed);
                Assert.Equal(DataRowAction.Change, _rowAction_Changed);


                // current - none; result - added
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 5, "mono 5" }, LoadOption.Upsert);
                Assert.Equal("mono 5", dt.Rows[4][1]);
                try
                {
                    object o = dt.Rows[4][1, DataRowVersion.Original];
//.........这里部分代码省略.........
开发者ID:dotnet,项目名称:corefx,代码行数:101,代码来源:DataTableLoadRowTest.cs

示例7: LoadRowTestPreserveChanges

        public void LoadRowTestPreserveChanges()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("name", typeof(string));

            dt.Rows.Add(new object[] { 1, "mono 1" });
            dt.Rows.Add(new object[] { 2, "mono 2" });
            dt.Rows.Add(new object[] { 3, "mono 3" });

            dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
            dt.AcceptChanges();
            try
            {
                SubscribeEvents(dt);

                // current - modified; new - modified
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 2, "mono test" }, LoadOption.PreserveChanges);
                Assert.Equal(3, dt.Rows.Count);
                Assert.Equal("mono test", dt.Rows[1][1]);
                Assert.Equal("mono test", dt.Rows[1][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[1], _rowInAction_Changing);
                Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[1], _rowInAction_Changed);
                Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);

                // current - none; new - unchanged
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 4, "mono 4" }, LoadOption.PreserveChanges);
                Assert.Equal(4, dt.Rows.Count);
                Assert.Equal("mono 4", dt.Rows[3][1]);
                Assert.Equal("mono 4", dt.Rows[3][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Unchanged, dt.Rows[3].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[3], _rowInAction_Changing);
                Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[3], _rowInAction_Changed);
                Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);


                dt.RejectChanges();

                // current - added; new - modified
                dt.Rows.Add(new object[] { 5, "mono 5" });
                ResetEventFlags();
                dt.LoadDataRow(new object[] { 5, "mono test" }, LoadOption.PreserveChanges);
                Assert.Equal(5, dt.Rows.Count);
                Assert.Equal("mono 5", dt.Rows[4][1]);
                Assert.Equal("mono test", dt.Rows[4][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Modified, dt.Rows[4].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[4], _rowInAction_Changing);
                Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[4], _rowInAction_Changed);
                Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changed);


                dt.RejectChanges();

                // current - deleted ; new - deleted ChangeOriginal
                ResetEventFlags();
                dt.Rows[1].Delete();
                Assert.True(_rowDeleting);
                Assert.True(_rowDeleted);
                Assert.Equal(_rowInAction_Deleting, dt.Rows[1]);
                Assert.Equal(_rowInAction_Deleted, dt.Rows[1]);
                Assert.Equal(_rowAction_Deleting, DataRowAction.Delete);
                Assert.Equal(_rowAction_Deleted, DataRowAction.Delete);
                dt.LoadDataRow(new object[] { 2, "mono deleted" }, LoadOption.PreserveChanges);
                Assert.Equal(5, dt.Rows.Count);
                Assert.Equal("mono deleted", dt.Rows[1][1, DataRowVersion.Original]);
                Assert.Equal(DataRowState.Deleted, dt.Rows[1].RowState);
                Assert.True(_rowChanging);
                Assert.Equal(dt.Rows[1], _rowInAction_Changing);
                Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changing);
                Assert.True(_rowChanged);
                Assert.Equal(dt.Rows[1], _rowInAction_Changed);
                Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changed);
            }
            finally
            {
                UnsubscribeEvents(dt);
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:90,代码来源:DataTableLoadRowTest.cs


注:本文中的DataTable.RejectChanges方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。