本文整理汇总了C#中DataTable.Copy方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.Copy方法的具体用法?C# DataTable.Copy怎么用?C# DataTable.Copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable.Copy方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
//.........这里部分代码省略.........
示例2: TestEquals_SameTableDiffViewProp
public void TestEquals_SameTableDiffViewProp()
{
DataTable table = new DataTable("table");
table.Columns.Add("col1", typeof(int));
table.Columns.Add("col2", typeof(int));
for (int i = 0; i < 5; ++i)
table.Rows.Add(new object[] { i, 100 + i });
DataView view1 = new DataView(table);
DataView view2 = new DataView(table);
object obj2 = view2;
Assert.False(view1.Equals(obj2));
Assert.True(view1.Equals(view1));
Assert.True(view2.Equals(view1));
view1.Sort = "col1 ASC";
Assert.False(view1.Equals(view2));
view2.Sort = "col1 ASC";
Assert.True(view1.Equals(view2));
view1.RowFilter = "col1 > 100";
Assert.False(view1.Equals(view2));
view1.RowFilter = "";
Assert.True(view1.Equals(view2));
view1.RowStateFilter = DataViewRowState.Added;
Assert.False(view1.Equals(view2));
view1.RowStateFilter = DataViewRowState.CurrentRows;
Assert.True(view1.Equals(view2));
view1.AllowDelete = !view2.AllowDelete;
Assert.False(view1.Equals(view2));
view1.AllowDelete = view2.AllowDelete;
Assert.True(view1.Equals(view2));
view1.AllowEdit = !view2.AllowEdit;
Assert.False(view1.Equals(view2));
view1.AllowEdit = view2.AllowEdit;
Assert.True(view1.Equals(view2));
view1.AllowNew = !view2.AllowNew;
Assert.False(view1.Equals(view2));
view1.AllowNew = view2.AllowNew;
Assert.True(view1.Equals(view2));
//ApplyDefaultSort doesnet affect the comparision
view1.ApplyDefaultSort = !view2.ApplyDefaultSort;
Assert.True(view1.Equals(view2));
DataTable table2 = table.Copy();
view1.Table = table2;
Assert.False(view1.Equals(view2));
view1.Table = table;
//well.. sort is set to null when Table is assigned..
view1.Sort = view2.Sort;
Assert.True(view1.Equals(view2));
}
示例3: GenerateTestData
private void GenerateTestData(out DataSet ds,
out DataTable dtMainInDS,
out DataTable dtChildInDS,
out DataTable dtMain)
{
ds = new DataSet("MyDataSet");
// Create a primary table and populate it with some data. Make a
// copy of the primary table and put it into the dataset.
dtMain = new DataTable("Main");
dtMain.Columns.Add(new DataColumn("ID", typeof(int)));
dtMain.Columns.Add(new DataColumn("Data", typeof(string)));
DataRow row = dtMain.NewRow();
row["ID"] = 1;
row["Data"] = "One";
dtMain.Rows.Add(row);
row = dtMain.NewRow();
row["ID"] = 2;
row["Data"] = "Two";
dtMain.Rows.Add(row);
row = dtMain.NewRow();
row["ID"] = 3;
row["Data"] = "Three";
dtMain.Rows.Add(row);
dtMainInDS = dtMain.Copy();
ds.Tables.Add(dtMainInDS);
// Create a child table. Make a copy of the child table and put
// it into the dataset.
dtChildInDS = new DataTable("Child");
dtChildInDS.Columns.Add(new DataColumn("ID", typeof(int)));
dtChildInDS.Columns.Add(new DataColumn("PID", typeof(int)));
dtChildInDS.Columns.Add(new DataColumn("ChildData", typeof(string)));
row = dtChildInDS.NewRow();
row["ID"] = 1;
row["PID"] = 1;
row["ChildData"] = "Parent1Child1";
dtChildInDS.Rows.Add(row);
row = dtChildInDS.NewRow();
row["ID"] = 2;
row["PID"] = 1;
row["ChildData"] = "Parent1Child2";
dtChildInDS.Rows.Add(row);
row = dtChildInDS.NewRow();
row["ID"] = 3;
row["PID"] = 2;
row["ChildData"] = "Parent2Child3";
dtChildInDS.Rows.Add(row);
ds.Tables.Add(dtChildInDS);
// Set up the relation in the dataset.
ds.Relations.Add(new DataRelation("MainToChild",
dtMainInDS.Columns["ID"],
dtChildInDS.Columns["PID"]));
}