本文整理汇总了C#中DataSet.Copy方法的典型用法代码示例。如果您正苦于以下问题:C# DataSet.Copy方法的具体用法?C# DataSet.Copy怎么用?C# DataSet.Copy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataSet
的用法示例。
在下文中一共展示了DataSet.Copy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Merge_ByDataTablePreserveMissingSchemaAction
public void Merge_ByDataTablePreserveMissingSchemaAction()
{
DataTable dt = DataProvider.CreateParentDataTable();
dt.TableName = "Table1";
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
//create target dataset (copy of source dataset)
DataSet dsTarget = new DataSet();
dsTarget.Tables.Add(dt.Copy());
//add new column (for checking MissingSchemaAction)
DataColumn dc = new DataColumn("NewColumn", typeof(float));
dt.Columns.Add(dc);
//Update row
string OldValue = dt.Select("ParentId=1")[0][1].ToString();
dt.Select("ParentId=1")[0][1] = "NewValue";
//delete rows
dt.Select("ParentId=2")[0].Delete();
//add row
object[] arrAddedRow = new object[] { 99, "NewRowValue1", "NewRowValue2", new DateTime(0), 0.5, true };
dt.Rows.Add(arrAddedRow);
#region "Merge(dt,true,MissingSchemaAction.Ignore )"
DataSet dsTarget1 = dsTarget.Copy();
dsTarget1.Merge(dt, true, MissingSchemaAction.Ignore);
// Merge true,Ignore - Column
Assert.Equal(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"));
// Merge true,Ignore - changed values
Assert.Equal(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1]);
// Merge true,Ignore - added values
Assert.Equal(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray);
// Merge true,Ignore - deleted row
Assert.Equal(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0);
#endregion
#region "Merge(dt,false,MissingSchemaAction.Ignore )"
dsTarget1 = dsTarget.Copy();
dsTarget1.Merge(dt, false, MissingSchemaAction.Ignore);
// Merge true,Ignore - Column
Assert.Equal(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"));
// Merge true,Ignore - changed values
Assert.Equal("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1]);
// Merge true,Ignore - added values
Assert.Equal(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray);
// Merge true,Ignore - deleted row
Assert.Equal(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length);
#endregion
#region "Merge(dt,true,MissingSchemaAction.Add )"
dsTarget1 = dsTarget.Copy();
dsTarget1.Merge(dt, true, MissingSchemaAction.Add);
// Merge true,Add - Column
Assert.Equal(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"));
// Merge true,Add - changed values
Assert.Equal(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1]);
// Merge true,Add - added values
Assert.Equal(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length);
// Merge true,Add - deleted row
Assert.Equal(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0);
#endregion
#region "Merge(dt,false,MissingSchemaAction.Add )"
dsTarget1 = dsTarget.Copy();
dsTarget1.Merge(dt, false, MissingSchemaAction.Add);
// Merge true,Add - Column
Assert.Equal(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"));
// Merge true,Add - changed values
Assert.Equal("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1]);
// Merge true,Add - added values
Assert.Equal(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length);
// Merge true,Add - deleted row
Assert.Equal(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length);
#endregion
}
示例2: Merge_PrimaryKeys_IncorrectOrder
public void Merge_PrimaryKeys_IncorrectOrder()
{
Assert.Throws<DataException>(() =>
{
var ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataTable table2 = ds.Tables.Add("table2");
DataColumn pcol = table1.Columns.Add("col1", typeof(int));
DataColumn pcol1 = table1.Columns.Add("col2", typeof(int));
DataColumn ccol = table2.Columns.Add("col1", typeof(int));
DataSet ds1 = ds.Copy();
table1.PrimaryKey = new DataColumn[] { pcol, pcol1 };
ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[1], ds1.Tables[0].Columns[0] };
// Though the key columns are the same, if the order is incorrect
// Exception must be raised
ds1.Merge(ds);
});
}
示例3: Merge_ByDataTable
public void Merge_ByDataTable()
{
//create source dataset
var ds = new DataSet();
//create datatable
DataTable dt = DataProvider.CreateParentDataTable();
dt.TableName = "Table1";
//add a copy of the datatable to the dataset
ds.Tables.Add(dt.Copy());
dt.TableName = "Table2";
//add primary key
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
ds.Tables.Add(dt.Copy());
//now the dataset hase two tables
//create target dataset (copy of source dataset)
DataSet dsTarget = ds.Copy();
dt = ds.Tables["Table2"];
//update existing row
dt.Select("ParentId=1")[0][1] = "OldValue1";
//add new row
object[] arrAddedRow = new object[] { 99, "NewValue1", "NewValue2", new DateTime(0), 0.5, true };
dt.Rows.Add(arrAddedRow);
//delete existing rows
foreach (DataRow dr in dt.Select("ParentId=2"))
{
dr.Delete();
}
// Merge - changed values
dsTarget.Merge(dt);
Assert.Equal("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1]);
// Merge - added values
Assert.Equal(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray);
// Merge - deleted row
Assert.Equal(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length);
//when merging a DataTable with TableName=null, GH throw null reference exception.
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Col1");
dt.Rows.Add(new object[] { 1 });
// Merge - add a table with no name
ds.Merge(dt);
Assert.Equal(1, ds.Tables.Count);
// Merge - add a table with no name - check Rows.Count
Assert.Equal(dt.Rows.Count, ds.Tables[0].Rows.Count);
}
示例4: Merge_ByDataSet
public void Merge_ByDataSet()
{
//create source dataset
var ds = new DataSet();
DataTable dt = DataProvider.CreateParentDataTable();
dt.TableName = "Table1";
ds.Tables.Add(dt.Copy());
dt.TableName = "Table2";
//add primary key
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
ds.Tables.Add(dt.Copy());
//create target dataset (copy of source dataset)
DataSet dsTarget = ds.Copy();
int iTable1RowsCount = dsTarget.Tables["Table1"].Rows.Count;
//Source - add another table, don't exists on the target dataset
ds.Tables.Add(new DataTable("SomeTable"));
ds.Tables["SomeTable"].Columns.Add("Id");
ds.Tables["SomeTable"].Rows.Add(new object[] { 777 });
//Target - add another table, don't exists on the source dataset
dsTarget.Tables.Add(new DataTable("SmallTable"));
dsTarget.Tables["SmallTable"].Columns.Add("Id");
dsTarget.Tables["SmallTable"].Rows.Add(new object[] { 777 });
//update existing row
ds.Tables["Table2"].Select("ParentId=1")[0][1] = "OldValue1";
//add new row
object[] arrAddedRow = new object[] { 99, "NewValue1", "NewValue2", new DateTime(0), 0.5, true };
ds.Tables["Table2"].Rows.Add(arrAddedRow);
//delete existing rows
foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
{
dr.Delete();
}
// Merge - changed values
dsTarget.Merge(ds);
Assert.Equal("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1]);
// Merge - added values
Assert.Equal(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray);
// Merge - deleted row
Assert.Equal(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length);
//Table1 rows count should be double (no primary key)
// Merge - Unchanged table 1
Assert.Equal(iTable1RowsCount * 2, dsTarget.Tables["Table1"].Rows.Count);
//SmallTable rows count should be the same
// Merge - Unchanged table 2
Assert.Equal(1, dsTarget.Tables["SmallTable"].Rows.Count);
//SomeTable - new table
// Merge - new table
Assert.Equal(true, dsTarget.Tables["SomeTable"] != null);
}
示例5: Merge_MissingConstraint
public void Merge_MissingConstraint()
{
DataSet ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataTable table2 = ds.Tables.Add("table2");
table1.Columns.Add("col1", typeof(int));
table2.Columns.Add("col1", typeof(int));
try
{
DataSet ds1 = ds.Copy();
DataSet ds2 = ds.Copy();
ds2.Tables[0].Constraints.Add("uc", ds2.Tables[0].Columns[0], false);
ds1.Merge(ds2, true, MissingSchemaAction.Error);
Assert.False(true);
}
catch (DataException e)
{
}
try
{
DataSet ds1 = ds.Copy();
DataSet ds2 = ds.Copy();
ds2.Tables[0].Constraints.Add("fk", ds2.Tables[0].Columns[0], ds2.Tables[1].Columns[0]);
ds1.Tables[0].Constraints.Add("uc", ds1.Tables[0].Columns[0], false);
ds1.Merge(ds2, true, MissingSchemaAction.Error);
Assert.False(true);
}
catch (DataException e)
{
}
try
{
DataSet ds1 = ds.Copy();
DataSet ds2 = ds.Copy();
ds2.Relations.Add("rel", ds2.Tables[0].Columns[0], ds2.Tables[1].Columns[0], false);
ds1.Merge(ds2, true, MissingSchemaAction.Error);
Assert.False(true);
}
catch (ArgumentException e)
{
}
}
示例6: CopyClone_RelationWithoutConstraints
public void CopyClone_RelationWithoutConstraints()
{
DataSet ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataTable table2 = ds.Tables.Add("table2");
DataColumn pcol = table1.Columns.Add("col1", typeof(int));
DataColumn ccol = table2.Columns.Add("col1", typeof(int));
DataRelation rel = ds.Relations.Add("rel1", pcol, ccol, false);
DataSet ds1 = ds.Copy();
DataSet ds2 = ds.Clone();
Assert.Equal(1, ds1.Relations.Count);
Assert.Equal(1, ds2.Relations.Count);
Assert.Equal(0, ds1.Tables[0].Constraints.Count);
Assert.Equal(0, ds1.Tables[1].Constraints.Count);
Assert.Equal(0, ds2.Tables[0].Constraints.Count);
Assert.Equal(0, ds2.Tables[1].Constraints.Count);
}
示例7: Merge_MissingEventHandler
public void Merge_MissingEventHandler()
{
Assert.Throws<DataException>(() =>
{
var ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataColumn pcol = table1.Columns.Add("col1", typeof(int));
DataColumn pcol1 = table1.Columns.Add("col2", typeof(int));
DataSet ds1 = ds.Copy();
table1.PrimaryKey = new DataColumn[] { pcol };
ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[1] };
// Exception shud be raised when handler is not set for MergeFailed Event
ds1.Merge(ds);
});
}
示例8: Merge_DuplicateConstraints
public void Merge_DuplicateConstraints()
{
DataSet ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataTable table2 = ds.Tables.Add("table2");
DataColumn pcol = table1.Columns.Add("col1", typeof(int));
DataColumn ccol = table2.Columns.Add("col1", typeof(int));
DataSet ds1 = ds.Copy();
DataRelation rel = ds.Relations.Add("rel1", pcol, ccol);
ds1.Tables[1].Constraints.Add("fk", ds1.Tables[0].Columns[0], ds1.Tables[1].Columns[0]);
// No Exceptions shud be thrown
ds.Merge(ds1);
Assert.Equal(1, table2.Constraints.Count);
}
示例9: Merge_DuplicateConstraints_1
public void Merge_DuplicateConstraints_1()
{
DataSet ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataTable table2 = ds.Tables.Add("table2");
DataColumn pcol = table1.Columns.Add("col1", typeof(int));
DataColumn ccol = table2.Columns.Add("col1", typeof(int));
DataColumn pcol1 = table1.Columns.Add("col2", typeof(int));
DataColumn ccol1 = table2.Columns.Add("col2", typeof(int));
DataSet ds1 = ds.Copy();
table2.Constraints.Add("fk", pcol, ccol);
ds1.Tables[1].Constraints.Add("fk", ds1.Tables[0].Columns["col2"], ds1.Tables[1].Columns["col2"]);
// No Exceptions shud be thrown
ds.Merge(ds1);
Assert.Equal(2, table2.Constraints.Count);
Assert.Equal("Constraint1", table2.Constraints[1].ConstraintName);
}
示例10: Merge_ByComplexDataSet
public void Merge_ByComplexDataSet()
{
//create source dataset
var ds = new DataSet();
ds.Tables.Add(DataProvider.CreateParentDataTable());
ds.Tables.Add(DataProvider.CreateChildDataTable());
ds.Tables["Child"].TableName = "Child2";
ds.Tables.Add(DataProvider.CreateChildDataTable());
//craete a target dataset to the merge operation
DataSet dsTarget = ds.Copy();
//craete a second target dataset to the merge operation
DataSet dsTarget1 = ds.Copy();
//------------------ make some changes in the second target dataset schema --------------------
//add primary key
dsTarget1.Tables["Parent"].PrimaryKey = new DataColumn[] { dsTarget1.Tables["Parent"].Columns["ParentId"] };
dsTarget1.Tables["Child"].PrimaryKey = new DataColumn[] { dsTarget1.Tables["Child"].Columns["ParentId"], dsTarget1.Tables["Child"].Columns["ChildId"] };
//add Foreign Key (different name)
dsTarget1.Tables["Child2"].Constraints.Add("Child2_FK_2", dsTarget1.Tables["Parent"].Columns["ParentId"], dsTarget1.Tables["Child2"].Columns["ParentId"]);
//add relation (different name)
//dsTarget1.Relations.Add("Parent_Child_1",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ParentId"]);
//------------------ make some changes in the source dataset schema --------------------
//add primary key
ds.Tables["Parent"].PrimaryKey = new DataColumn[] { ds.Tables["Parent"].Columns["ParentId"] };
ds.Tables["Child"].PrimaryKey = new DataColumn[] { ds.Tables["Child"].Columns["ParentId"], ds.Tables["Child"].Columns["ChildId"] };
//unique column
ds.Tables["Parent"].Columns["String2"].Unique = true; //will not be merged
//add Foreign Key
ds.Tables["Child2"].Constraints.Add("Child2_FK", ds.Tables["Parent"].Columns["ParentId"], ds.Tables["Child2"].Columns["ParentId"]);
//add relation
ds.Relations.Add("Parent_Child", ds.Tables["Parent"].Columns["ParentId"], ds.Tables["Child"].Columns["ParentId"]);
//add allow null constraint
ds.Tables["Parent"].Columns["ParentBool"].AllowDBNull = false; //will not be merged
//add Indentity column
ds.Tables["Parent"].Columns.Add("Indentity", typeof(int));
ds.Tables["Parent"].Columns["Indentity"].AutoIncrement = true;
ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep = 2;
//modify default value
ds.Tables["Child"].Columns["String1"].DefaultValue = "Default"; //will not be merged
//remove column
ds.Tables["Child"].Columns.Remove("String2"); //will not be merged
//-------------------- begin to check ----------------------------------------------
// merge 1 - make sure the merge method invoked without exceptions
dsTarget.Merge(ds);
Assert.Equal("Success", "Success");
CompareResults_1("merge 1", ds, dsTarget);
//merge again,
// merge 2 - make sure the merge method invoked without exceptions
dsTarget.Merge(ds);
Assert.Equal("Success", "Success");
CompareResults_1("merge 2", ds, dsTarget);
// merge second dataset - make sure the merge method invoked without exceptions
dsTarget1.Merge(ds);
Assert.Equal("Success", "Success");
CompareResults_2("merge 3", ds, dsTarget1);
}
示例11: Merge_ByDataSetPreserveMissingSchemaAction
public void Merge_ByDataSetPreserveMissingSchemaAction()
{
//create source dataset
var ds = new DataSet();
DataTable dt = DataProvider.CreateParentDataTable();
dt.TableName = "Table1";
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
//add table to dataset
ds.Tables.Add(dt.Copy());
dt = ds.Tables[0];
//create target dataset (copy of source dataset)
DataSet dsTarget = ds.Copy();
//add new column (for checking MissingSchemaAction)
DataColumn dc = new DataColumn("NewColumn", typeof(float));
//make the column to be primary key
dt.Columns.Add(dc);
//add new table (for checking MissingSchemaAction)
ds.Tables.Add(new DataTable("NewTable"));
ds.Tables["NewTable"].Columns.Add("NewColumn1", typeof(int));
ds.Tables["NewTable"].Columns.Add("NewColumn2", typeof(long));
ds.Tables["NewTable"].Rows.Add(new object[] { 1, 2 });
ds.Tables["NewTable"].Rows.Add(new object[] { 3, 4 });
ds.Tables["NewTable"].PrimaryKey = new DataColumn[] { ds.Tables["NewTable"].Columns["NewColumn1"] };
#region "ds,false,MissingSchemaAction.Add)"
DataSet dsTarget1 = dsTarget.Copy();
dsTarget1.Merge(ds, false, MissingSchemaAction.Add);
// Merge MissingSchemaAction.Add - Column
Assert.Equal(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"));
// Merge MissingSchemaAction.Add - Table
Assert.Equal(true, dsTarget1.Tables.Contains("NewTable"));
//failed, should be success by MSDN Library documentation
// // Merge MissingSchemaAction.Add - PrimaryKey
// Assert.Equal(0, dsTarget1.Tables["NewTable"].PrimaryKey.Length);
#endregion
#region "ds,false,MissingSchemaAction.AddWithKey)"
//MissingSchemaAction.Add,MissingSchemaAction.AddWithKey - behave the same, checked only Add
// DataSet dsTarget2 = dsTarget.Copy();
// dsTarget2.Merge(ds,false,MissingSchemaAction.AddWithKey);
// // Merge MissingSchemaAction.AddWithKey - Column
// Assert.Equal(true, dsTarget2.Tables["Table1"].Columns.Contains("NewColumn"));
//
// // Merge MissingSchemaAction.AddWithKey - Table
// Assert.Equal(true, dsTarget2.Tables.Contains("NewTable"));
//
// // Merge MissingSchemaAction.AddWithKey - PrimaryKey
// Assert.Equal(dsTarget2.Tables["NewTable"].Columns["NewColumn1"], dsTarget2.Tables["NewTable"].PrimaryKey[0]);
#endregion
#region "ds,false,MissingSchemaAction.Ignore )"
DataSet dsTarget4 = dsTarget.Copy();
dsTarget4.Merge(ds, false, MissingSchemaAction.Ignore);
// Merge MissingSchemaAction.Ignore - Column
Assert.Equal(false, dsTarget4.Tables["Table1"].Columns.Contains("NewColumn"));
// Merge MissingSchemaAction.Ignore - Table
Assert.Equal(false, dsTarget4.Tables.Contains("NewTable"));
#endregion
}
示例12: Merge_ByDataSetPreserve
public void Merge_ByDataSetPreserve()
{
//create source dataset
var ds = new DataSet();
DataTable dt = DataProvider.CreateParentDataTable();
dt.TableName = "Table1";
ds.Tables.Add(dt.Copy());
dt.TableName = "Table2";
//add primary key
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
ds.Tables.Add(dt.Copy());
//create target dataset (copy of source dataset)
DataSet dsTarget1 = ds.Copy();
DataSet dsTarget2 = ds.Copy();
int iTable1RowsCount = dsTarget1.Tables["Table1"].Rows.Count;
//update existing row
string oldValue = ds.Tables["Table2"].Select("ParentId=1")[0][1].ToString();
ds.Tables["Table2"].Select("ParentId=1")[0][1] = "NewValue";
//add new row
object[] arrAddedRow = new object[] { 99, "NewValue1", "NewValue2", new DateTime(0), 0.5, true };
ds.Tables["Table2"].Rows.Add(arrAddedRow);
//delete existing rows
int iDeleteLength = dsTarget1.Tables["Table2"].Select("ParentId=2").Length;
foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
{
dr.Delete();
}
#region "Merge(ds,true)"
//only new added rows are merged (preserveChanges = true)
dsTarget1.Merge(ds, true);
// Merge - changed values
Assert.Equal(oldValue, dsTarget1.Tables["Table2"].Select("ParentId=1")[0][1]);
// Merge - added values
Assert.Equal(arrAddedRow, dsTarget1.Tables["Table2"].Select("ParentId=99")[0].ItemArray);
// Merge - deleted row
Assert.Equal(iDeleteLength, dsTarget1.Tables["Table2"].Select("ParentId=2").Length);
#endregion
#region "Merge(ds,false)"
//all changes are merged (preserveChanges = false)
dsTarget2.Merge(ds, false);
// Merge - changed values
Assert.Equal("NewValue", dsTarget2.Tables["Table2"].Select("ParentId=1")[0][1]);
// Merge - added values
Assert.Equal(arrAddedRow, dsTarget2.Tables["Table2"].Select("ParentId=99")[0].ItemArray);
// Merge - deleted row
Assert.Equal(0, dsTarget2.Tables["Table2"].Select("ParentId=2").Length);
#endregion
}
示例13: Copy
public void Copy()
{
DataSet ds = new DataSet(), dsTarget = null;
ds.Tables.Add(DataProvider.CreateParentDataTable());
ds.Tables.Add(DataProvider.CreateChildDataTable());
ds.Relations.Add(new DataRelation("myRelation", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]));
ds.Tables[0].Rows.Add(new object[] { 9, "", "" });
ds.Tables[1].Columns[2].ReadOnly = true;
ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[0], ds.Tables[0].Columns[1] };
//copy data and schema
// Copy 1
dsTarget = ds.Copy();
//Assert.Equal(ds.GetXmlSchema(), dsTarget.GetXmlSchema() );
//using my function because GetXmlSchema in not implemented in java
Assert.Equal(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema(dsTarget));
// Copy 2
Assert.Equal(true, dsTarget.GetXml() == ds.GetXml());
}
示例14: Merge_MissingColumn
public void Merge_MissingColumn()
{
Assert.Throws<DataException>(() =>
{
var ds = new DataSet();
DataTable table1 = ds.Tables.Add("table1");
DataTable table2 = ds.Tables.Add("table2");
table1.Columns.Add("col1", typeof(int));
table2.Columns.Add("col1", typeof(int));
DataSet ds1 = ds.Copy();
ds1.Tables[0].Columns.Add("col2");
ds.Merge(ds1, true, MissingSchemaAction.Error);
});
}
示例15: CloneCopy_TestForeignKeyConstraints
public void CloneCopy_TestForeignKeyConstraints()
{
DataTable dirTable = new DataTable("Directories");
DataColumn dir_UID = new DataColumn("UID", typeof(int));
dir_UID.Unique = true;
dir_UID.AllowDBNull = false;
dirTable.Columns.Add(dir_UID);
// Build a simple Files table
DataTable fileTable = new DataTable("Files");
DataColumn file_DirID = new DataColumn("DirectoryID", typeof(int));
file_DirID.Unique = false;
file_DirID.AllowDBNull = false;
fileTable.Columns.Add(file_DirID);
// Build the DataSet
DataSet ds = new DataSet("TestDataset");
ds.Tables.Add(dirTable);
ds.Tables.Add(fileTable);
// Add a foreign key constraint
DataColumn[] parentColumns = new DataColumn[1];
parentColumns[0] = ds.Tables["Directories"].Columns["UID"];
DataColumn[] childColumns = new DataColumn[1];
childColumns[0] = ds.Tables["Files"].Columns["DirectoryID"];
ForeignKeyConstraint fk = new ForeignKeyConstraint("FK_Test", parentColumns, childColumns);
ds.Tables["Files"].Constraints.Add(fk);
ds.EnforceConstraints = true;
Assert.Equal(1, ds.Tables["Directories"].Constraints.Count);
Assert.Equal(1, ds.Tables["Files"].Constraints.Count);
// check clone works fine
DataSet cloned_ds = ds.Clone();
Assert.Equal(1, cloned_ds.Tables["Directories"].Constraints.Count);
Assert.Equal(1, cloned_ds.Tables["Files"].Constraints.Count);
ForeignKeyConstraint clonedFk = (ForeignKeyConstraint)cloned_ds.Tables["Files"].Constraints[0];
Assert.Equal("FK_Test", clonedFk.ConstraintName);
Assert.Equal(1, clonedFk.Columns.Length);
Assert.Equal("DirectoryID", clonedFk.Columns[0].ColumnName);
UniqueConstraint clonedUc = (UniqueConstraint)cloned_ds.Tables["Directories"].Constraints[0];
UniqueConstraint origUc = (UniqueConstraint)ds.Tables["Directories"].Constraints[0];
Assert.Equal(origUc.ConstraintName, clonedUc.ConstraintName);
Assert.Equal(1, clonedUc.Columns.Length);
Assert.Equal("UID", clonedUc.Columns[0].ColumnName);
// check copy works fine
DataSet copy_ds = ds.Copy();
Assert.Equal(1, copy_ds.Tables["Directories"].Constraints.Count);
Assert.Equal(1, copy_ds.Tables["Files"].Constraints.Count);
ForeignKeyConstraint copyFk = (ForeignKeyConstraint)copy_ds.Tables["Files"].Constraints[0];
Assert.Equal("FK_Test", copyFk.ConstraintName);
Assert.Equal(1, copyFk.Columns.Length);
Assert.Equal("DirectoryID", copyFk.Columns[0].ColumnName);
UniqueConstraint copyUc = (UniqueConstraint)copy_ds.Tables["Directories"].Constraints[0];
origUc = (UniqueConstraint)ds.Tables["Directories"].Constraints[0];
Assert.Equal(origUc.ConstraintName, copyUc.ConstraintName);
Assert.Equal(1, copyUc.Columns.Length);
Assert.Equal("UID", copyUc.Columns[0].ColumnName);
}