Can a DataSet be populated with data from two sources?

I have DataSetand reading data from two sources. One table from an XML file and another table from a Firebird SQL database. What I'm trying to get is only one table, in which there are all columns from the XML file and several fields from SQL data in one table. Both tables have the same unique key field, so it can be easily merged. I would like to bind all the fields of this table to the fields of the form.

Is this possible, as described, or I don’t see that there is a simpler solution to my problem?

Edit : To show that I'm trying to make some extra code.

DataSet dataSet = new DataSet();

DataTable table1 = new DataTable("test1", "test1");
table1.Columns.Add("id");
table1.Columns.Add("name");
table1.Columns[0].Unique = true;

table1.Rows.Add(new object[2] { 1, "name1" });
table1.Rows.Add(new object[2] { 2, "name2" });

DataTable table2 = new DataTable("test2", "test2");
table2.Columns.Add("id");
table2.Columns.Add("thing");
table2.Columns[0].Unique = true;

table2.Rows.Add(new object[2] { 1, "thing1" });
table2.Rows.Add(new object[2] { 2, "thing2" });

dataSet.Tables.Add(table1);
dataSet.Tables[0].Merge(table2, false);

, ConstraintException. id, , table1 table2. ?

2: PrimaryKey, .

xmlData.Tables[0].PrimaryKey = new[] { xmlData.Tables[0].Columns["usnr"] };
dbData.PrimaryKey = new[] { dbData.Columns["usid"] };
xmlData.Tables[0].Merge(dbData, true, MissingSchemaAction.Add);

xmlData - DataSet, XML. id, usnr . dbData - DataTable, db id, usid, . id . usnr usid , GetType().

xmlData.Tables[0].Merge(dbData, true, MissingSchemaAction.Add);, DataException

<target>.ID and <source>.ID have conflicting properties: DataType property mismatch.

, id, , , , primaryIey . NullReferenceException . , ?

+4
3

...

table1.Columns[0].Unique = true;
table2.Columns[0].Unique = true;

... :

table1.PrimaryKey = new[] { table1.Columns[0] };
table2.PrimaryKey = new[] { table2.Columns[0] };

. , , . :

id name  thing
== ===== ======
1  name1 thing1 
2  name2 thing2 

, . , . , , . Microsoft .

, usnr, usid .

+2

Linq DataTables :

.........
.........
dataSet.Tables.Add(table1);
//dataSet.Tables[0].Merge(table2, false);

var collection = from t1 in dataSet.Tables[0].AsEnumerable()
                 join t2 in table2.AsEnumerable()
                 on t1["id"] equals t2["id"]
                 select new
                 {
                     ID = t1["id"],
                     Name = t1["name"],
                     Thing = t2["thing"]
                 };

DataTable result = new DataTable("Result");
result.Columns.Add("ID", typeof(string));
result.Columns.Add("Name", typeof(string));
result.Columns.Add("Thing", typeof(string));

foreach (var item in collection)
{
    result.Rows.Add(item.ID, item.Name, item.Thing);
}

DataGridView , , :

dataGridView1.DataSource = result;

enter image description here

+1

Here you cannot combine these two data tables together. You need to combine the data in these two tables, iterating over each of them.

Create a new data table containing all columns (Id, Name, Thing). Then write down this table reading the other two.

0
source

All Articles