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


C# DataTable.Clone方法代码示例

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


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

示例1: Remove

        public void Remove()
        {
            //prepare a DataSet with DataTable to be checked
            DataTable dtSource = new DataTable();
            dtSource.Columns.Add("Col_0", typeof(int));
            dtSource.Columns.Add("Col_1", typeof(int));
            dtSource.Columns.Add("Col_2", typeof(int));
            dtSource.Rows.Add(new object[] { 0, 1, 2 });

            DataTable dt = null;

            //------Check Remove first column---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            dt.Columns.Remove(dt.Columns[0].ColumnName);
            Assert.Equal(2, dt.Columns.Count);
            Assert.Equal(false, dt.Columns.Contains("Col_0"));
            Assert.Equal(1, dt.Rows[0][0]);
            Assert.Equal(2, dt.Rows[0][1]);



            //------Check Remove middle column---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            dt.Columns.Remove(dt.Columns[1].ColumnName);
            Assert.Equal(2, dt.Columns.Count);
            Assert.Equal(false, dt.Columns.Contains("Col_1"));
            Assert.Equal(0, dt.Rows[0][0]);
            Assert.Equal(2, dt.Rows[0][1]);


            //------Check Remove last column---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            dt.Columns.Remove(dt.Columns[2].ColumnName);

            Assert.Equal(2, dt.Columns.Count);
            Assert.Equal(false, dt.Columns.Contains("Col_2"));
            Assert.Equal(0, dt.Rows[0][0]);
            Assert.Equal(1, dt.Rows[0][1]);


            //------Check Remove column exception---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            Assert.Throws<ArgumentException>(() =>
            {
                dt.Columns.Remove("NotExist");
            });

            dt.Columns.Clear();

            Assert.Throws<ArgumentException>(() =>
            {
                dt.Columns.Remove("Col_0");
            });
        }
开发者ID:dotnet,项目名称:corefx,代码行数:62,代码来源:DataColumnCollectionTest2.cs

示例2: TestRemove_ByDataColumn

        public void TestRemove_ByDataColumn()
        {
            //prepare a DataSet with DataTable to be checked
            DataTable dtSource = new DataTable();
            dtSource.Columns.Add("Col_0", typeof(int));
            dtSource.Columns.Add("Col_1", typeof(int));
            dtSource.Columns.Add("Col_2", typeof(int));
            dtSource.Rows.Add(new object[] { 0, 1, 2 });

            DataTable dt = null;

            //------Check Remove first column---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            dt.Columns.Remove(dt.Columns[0]);
            // Remove first column - check column count
            Assert.Equal(2, dt.Columns.Count);

            // Remove first column - check column removed
            Assert.Equal(false, dt.Columns.Contains("Col_0"));

            // Remove first column - check column 0 data
            Assert.Equal(1, dt.Rows[0][0]);

            // Remove first column - check column 1 data
            Assert.Equal(2, dt.Rows[0][1]);

            //------Check Remove middle column---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            dt.Columns.Remove(dt.Columns[1]);
            // Remove middle column - check column count
            Assert.Equal(2, dt.Columns.Count);

            // Remove middle column - check column removed
            Assert.Equal(false, dt.Columns.Contains("Col_1"));

            // Remove middle column - check column 0 data
            Assert.Equal(0, dt.Rows[0][0]);

            // Remove middle column - check column 1 data
            Assert.Equal(2, dt.Rows[0][1]);

            //------Check Remove last column---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);

            dt.Columns.Remove(dt.Columns[2]);
            // Remove last column - check column count
            Assert.Equal(2, dt.Columns.Count);

            // Remove last column - check column removed
            Assert.Equal(false, dt.Columns.Contains("Col_2"));

            // Remove last column - check column 0 data
            Assert.Equal(0, dt.Rows[0][0]);

            // Remove last column - check column 1 data
            Assert.Equal(1, dt.Rows[0][1]);

            //------Check Remove column exception---------
            dt = dtSource.Clone();
            dt.ImportRow(dtSource.Rows[0]);
            Assert.Throws<ArgumentException>(() =>
            {
                DataColumn dc = new DataColumn();
                dt.Columns.Remove(dc);
            });
        }
开发者ID:dotnet,项目名称:corefx,代码行数:71,代码来源:DataColumnCollectionTest2.cs

示例3: CloneExtendedProperties2

        public void CloneExtendedProperties2()
        {
            DataTable table1 = new DataTable("Table1");

            DataColumn c1 = table1.Columns.Add("c1");
            DataColumn c2 = table1.Columns.Add("c2");

            c1.Expression = "'hello ' + c2";

            DataTable t2 = table1.Clone(); // this should not cause an exception
        }
开发者ID:dotnet,项目名称:corefx,代码行数:11,代码来源:DataTableTest.cs

示例4: CloneCopyTest

        public void CloneCopyTest()
        {
            DataTable table = new DataTable();
            table.TableName = "Table#1";
            DataTable table1 = new DataTable();
            table1.TableName = "Table#2";

            table.AcceptChanges();

            DataSet set = new DataSet("Data Set#1");
            set.DataSetName = "Dataset#1";
            set.Tables.Add(table);
            set.Tables.Add(table1);

            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 = "Id";
            col.DataType = typeof(int);
            table1.Columns.Add(col);

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

            col = new DataColumn();
            col.ColumnName = "Name";
            col.DataType = typeof(string);
            table1.Columns.Add(col);
            DataRow row = table.NewRow();
            row["Id"] = 147;
            row["name"] = "Abc";
            row.RowError = "Error#1";
            table.Rows.Add(row);

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

            table.CaseSensitive = true;
            table1.CaseSensitive = true;
            table.MinimumCapacity = 100;
            table.Prefix = "PrefixNo:1";
            table.Namespace = "Namespace#1";
            table.DisplayExpression = "Id / Name + (Id * Id)";
            DataColumn[] colArray = { table.Columns[0] };
            table.PrimaryKey = colArray;
            table.ExtendedProperties.Add("TimeStamp", DateTime.Now);

            row = table1.NewRow();
            row["Name"] = "Abc";
            row["Id"] = 147;
            table1.Rows.Add(row);

            row = table1.NewRow();
            row["Id"] = 47;
            row["Name"] = "Efg";
            table1.Rows.Add(row);

            DataRelation dr = new DataRelation("DR", table.Columns[0], table1.Columns[0]);
            set.Relations.Add(dr);

            //Testing properties of clone
            DataTable cloneTable = table.Clone();
            Assert.True(cloneTable.CaseSensitive);
            Assert.Equal(0, cloneTable.ChildRelations.Count);
            Assert.Equal(0, cloneTable.ParentRelations.Count);
            Assert.Equal(2, cloneTable.Columns.Count);
            Assert.Equal(1, cloneTable.Constraints.Count);
            Assert.Equal("Id / Name + (Id * Id)", cloneTable.DisplayExpression);
            Assert.Equal(1, cloneTable.ExtendedProperties.Count);
            Assert.False(cloneTable.HasErrors);
            Assert.Equal(100, cloneTable.MinimumCapacity);
            Assert.Equal("Namespace#1", cloneTable.Namespace);
            Assert.Equal("PrefixNo:1", cloneTable.Prefix);
            Assert.Equal("Id", cloneTable.PrimaryKey[0].ColumnName);
            Assert.Equal(0, cloneTable.Rows.Count);
            Assert.Equal("Table#1", cloneTable.TableName);

            //Testing properties of copy
            DataTable copyTable = table.Copy();
            Assert.True(copyTable.CaseSensitive);
            Assert.Equal(0, copyTable.ChildRelations.Count);
            Assert.Equal(0, copyTable.ParentRelations.Count);
            Assert.Equal(2, copyTable.Columns.Count);
            Assert.Equal(1, copyTable.Constraints.Count);
            Assert.Equal("Id / Name + (Id * Id)", copyTable.DisplayExpression);
            Assert.Equal(1, copyTable.ExtendedProperties.Count);
            Assert.True(copyTable.HasErrors);
            Assert.Equal(100, copyTable.MinimumCapacity);
            Assert.Equal("Namespace#1", copyTable.Namespace);
            Assert.Equal("PrefixNo:1", copyTable.Prefix);
//.........这里部分代码省略.........
开发者ID:dotnet,项目名称:corefx,代码行数:101,代码来源:DataTableTest.cs

示例5: CloneExtendedProperties

        public void CloneExtendedProperties()
        {
            DataTable t1 = new DataTable("t1");
            DataColumn c1 = t1.Columns.Add("c1");
            c1.ExtendedProperties.Add("Company", "Xamarin");

            DataTable t2 = t1.Clone();
            Assert.Equal("Xamarin", t1.Columns["c1"].ExtendedProperties["Company"]);
            Assert.Equal("Xamarin", t2.Columns["c1"].ExtendedProperties["Company"]);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:DataTableTest.cs


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