本文整理汇总了C#中DataSet.Merge方法的典型用法代码示例。如果您正苦于以下问题:C# DataSet.Merge方法的具体用法?C# DataSet.Merge怎么用?C# DataSet.Merge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataSet
的用法示例。
在下文中一共展示了DataSet.Merge方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MergeTest_SameDataSet_536194
public void MergeTest_SameDataSet_536194()
{
DataSet dataSet = new DataSet("Test");
DataTable dataTable = new DataTable("Test");
dataTable.Columns.Add("Test");
dataTable.Rows.Add("Test");
dataSet.Tables.Add(dataTable);
dataSet.Merge(dataTable);
Assert.Equal(1, dataSet.Tables.Count);
}
示例2: 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);
}
示例3: MergeTest_ColumnTypeMismatch
public void MergeTest_ColumnTypeMismatch()
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(new DataTable());
dataSet.Tables[0].Columns.Add(new DataColumn("id", typeof(int)));
dataSet.Tables[0].Columns.Add(new DataColumn("name", typeof(string)));
DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add(new DataColumn("id", typeof(string)));
try
{
ds.Merge(dataSet, true, MissingSchemaAction.Add);
Assert.False(true);
}
catch (DataException e) { }
ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add(new DataColumn("id", typeof(string)));
ds.Merge(dataSet, true, MissingSchemaAction.Ignore);
Assert.Equal("Table1", ds.Tables[0].TableName);
Assert.Equal(1, ds.Tables.Count);
Assert.Equal(1, ds.Tables[0].Columns.Count);
Assert.Equal(typeof(string), ds.Tables[0].Columns[0].DataType);
}
示例4: 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);
});
}
示例5: CompareResults_2
private void CompareResults_2(string Msg, DataSet ds, DataSet dsTarget)
{
// check Parent Primary key length
Assert.Equal(dsTarget.Tables["Parent"].PrimaryKey.Length, ds.Tables["Parent"].PrimaryKey.Length);
// check Child Primary key length
Assert.Equal(dsTarget.Tables["Child"].PrimaryKey.Length, ds.Tables["Child"].PrimaryKey.Length);
// check Parent Primary key columns
Assert.Equal(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName);
// check Child Primary key columns[0]
Assert.Equal(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName);
// check Child Primary key columns[1]
Assert.Equal(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName);
// check Parent Unique columns
Assert.Equal(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique);
// check Child2 Foreign Key name
Assert.Equal("Child2_FK_2", dsTarget.Tables["Child2"].Constraints[0].ConstraintName);
// check dataset relation count
Assert.Equal(dsTarget.Relations.Count, ds.Relations.Count);
// check dataset relation - Parent column
Assert.Equal(dsTarget.Relations[0].ParentColumns[0].ColumnName, ds.Relations[0].ParentColumns[0].ColumnName);
// check dataset relation - Child column
Assert.Equal(dsTarget.Relations[0].ChildColumns[0].ColumnName, ds.Relations[0].ChildColumns[0].ColumnName);
// check allow null constraint
Assert.Equal(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull);
// check Indentity column
Assert.Equal(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"));
// check Indentity column - AutoIncrementStep
Assert.Equal(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep);
// check Indentity column - AutoIncrement
Assert.Equal(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement);
// check Indentity column - DefaultValue
Assert.Equal(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value);
// check remove colum
Assert.Equal(true, dsTarget.Tables["Child"].Columns.Contains("String2"));
// Check Relation.Nested value
DataSet orig = new DataSet();
DataTable parent = orig.Tables.Add("Parent");
parent.Columns.Add("Id", typeof(int));
parent.Columns.Add("col1", typeof(string));
parent.Rows.Add(new object[] { 0, "aaa" });
DataTable child = orig.Tables.Add("Child");
child.Columns.Add("ParentId", typeof(int));
child.Columns.Add("col1", typeof(string));
child.Rows.Add(new object[] { 0, "bbb" });
orig.Relations.Add("Parent_Child", parent.Columns["Id"], child.Columns["ParentId"]);
orig.Relations["Parent_Child"].Nested = true;
DataSet merged = new DataSet();
merged.Merge(orig);
Assert.Equal(orig.Relations["Parent_Child"].Nested, merged.Relations["Parent_Child"].Nested);
}
示例6: Merge_ConstraintsFromReadXmlSchema
public void Merge_ConstraintsFromReadXmlSchema()
{
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(
@"<MyDataSet>
<xs:schema id=""MyDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema""
xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
<xs:element name=""MyDataSet"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Main"" msdata:UseCurrentLocale=""true"">
<xs:complexType>
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""Main"">
<xs:complexType>
<xs:sequence>
<xs:element name=""ID"" type=""xs:int"" minOccurs=""0"" />
<xs:element name=""Data"" type=""xs:string"" minOccurs=""0"" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=""Child"">
<xs:complexType>
<xs:sequence>
<xs:element name=""ID"" type=""xs:int"" minOccurs=""0"" />
<xs:element name=""PID"" type=""xs:int"" minOccurs=""0"" />
<xs:element name=""ChildData"" type=""xs:string"" minOccurs=""0"" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name=""Constraint1"">
<xs:selector xpath="".//Main"" />
<xs:field xpath=""ID"" />
</xs:unique>
<xs:keyref name=""MainToChild"" refer=""Constraint1"">
<xs:selector xpath="".//Child"" />
<xs:field xpath=""PID"" />
</xs:keyref>
</xs:element>
</xs:schema>
<Main>
<ID>1</ID>
<Data>One</Data>
</Main>
<Main>
<ID>2</ID>
<Data>Two</Data>
</Main>
<Main>
<ID>3</ID>
<Data>Three</Data>
</Main>
<Child>
<ID>1</ID>
<PID>1</PID>
<ChildData>Parent1Child1</ChildData>
</Child>
<Child>
<ID>2</ID>
<PID>1</PID>
<ChildData>Parent1Child2</ChildData>
</Child>
<Child>
<ID>3</ID>
<PID>2</PID>
<ChildData>Parent2Child3</ChildData>
</Child>
</MyDataSet>"));
DataSet ds2 = new DataSet();
ds2.Merge(ds, true, MissingSchemaAction.AddWithKey);
DataRelation c = ds2.Tables[0].ChildRelations[0];
Assert.NotNull(c.ParentKeyConstraint);
Assert.NotNull(c.ChildKeyConstraint);
}
示例7: 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);
}
示例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_RelationWithoutConstraints
public void Merge_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));
DataSet ds1 = ds.Copy();
DataRelation rel = ds1.Relations.Add("rel1", ds1.Tables[0].Columns[0],
ds1.Tables[1].Columns[0], false);
ds.Merge(ds1);
Assert.Equal(1, ds.Relations.Count);
Assert.Equal(0, ds.Tables[0].Constraints.Count);
Assert.Equal(0, ds.Tables[1].Constraints.Count);
}
示例10: Merge_ByDataRowsNoPreserveIgnoreMissingSchema
public void Merge_ByDataRowsNoPreserveIgnoreMissingSchema()
{
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());
DataRow[] drArr = new DataRow[3];
//Update row
string OldValue = dt.Select("ParentId=1")[0][1].ToString();
drArr[0] = dt.Select("ParentId=1")[0];
drArr[0][1] = "NewValue";
//delete rows
drArr[1] = dt.Select("ParentId=2")[0];
drArr[1].Delete();
//add row
drArr[2] = dt.NewRow();
drArr[2].ItemArray = new object[] { 99, "NewRowValue1", "NewRowValue2" };
dt.Rows.Add(drArr[2]);
dsTarget.Merge(drArr, false, MissingSchemaAction.Ignore);
// Merge update row
Assert.Equal("NewValue", dsTarget.Tables["Table1"].Select("ParentId=1")[0][1]);
// Merge added row
Assert.Equal(1, dsTarget.Tables["Table1"].Select("ParentId=99").Length);
// Merge deleted row
Assert.Equal(0, dsTarget.Tables["Table1"].Select("ParentId=2").Length);
}