我在写一个连接不同数据库系统的c#应用程序。这些系统可以是平面文件数据库,Oracle,Sql,Excel文件,分机。 C#应用程序的工作是提供一个可以在一个位置使所有这些源可用的途径。所以基本上,应用程序接受各个数据库系统的查询和连接设置列表,并收集一堆结果。有没有一种简单的方法,在C#中的INNER连接,外部连接,左外部连接,右外部连接或UNION两个(或更多)DataTables?
目标是输出单个DataTable,并将所有这些查询的结果一起加入/组合在一起(取决于设置)。 C#提供了一种简单的方法来在DataTable列表上执行任何联合/联合操作吗?
例如:
Table1:
__________________________________________________________
|tb1_pk_id| tb1_name | tb1_data1 | tb1_data2 |
|---------|---------------|---------------|---------------|
| 1 | tb1name_blah1 | tb1dat1_blah1 | tb1dat2blah1 |
| 2 | tb1name_blah2 | tb1dat1_blah2 | tb1dat2blah2 |
| 3 | tb1name_blah3 | tb1dat1_blah3 | tb1dat2blah3 |
-----------------------------------------------------------
Table2:
__________________________________________________________
|tb2_pk_id| tb2_name | tb2_data1 | tb2_data2 |
|---------|---------------|---------------|---------------|
| 1 | tb2name_blah1 | tb2dat1_blah1 | tb2dat2blah1 |
| 2 | tb2name_blah2 | tb2dat1_blah2 | tb2dat2blah2 |
| 3 | tb2name_blah3 | tb2dat1_blah3 | tb2dat2blah3 |
-----------------------------------------------------------
Join Results:
__________________________________________________________ _______________________________________________
|tb1_pk_id| tb1_name | tb1_data1 | tb1_data2 | tb2_name | tb2_data1 | tb2_data2 |
|---------|---------------|---------------|---------------|---------------|---------------|---------------|
| 1 | tb1name_blah1 | tb1dat1_blah1 | tb1dat2blah1 | tb2name_blah1 | tb2dat1_blah1 | tb2dat2blah1 |
| 2 | tb1name_blah2 | tb1dat1_blah2 | tb1dat2blah2 | tb2name_blah2 | tb2dat1_blah2 | tb2dat2blah2 |
| 3 | tb1name_blah3 | tb1dat1_blah3 | tb1dat2blah3 | tb2name_blah3 | tb2dat1_blah3 | tb2dat2blah3 |
-----------------------------------------------------------------------------------------------------------
到目前为止,我已经找到了下面的代码在线(here)做一个合并上的所有数据:
private DataTable MergeAll(IList<DataTable> tables, String primaryKeyColumn)
{
if (!tables.Any())
throw new ArgumentException("Tables must not be empty", "tables");
if (primaryKeyColumn != null)
foreach (DataTable t in tables)
if (!t.Columns.Contains(primaryKeyColumn))
throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");
if (tables.Count == 1)
return tables[0];
DataTable table = new DataTable("TblUnion");
table.BeginLoadData(); // Turns off notifications, index maintenance, and constraints while loading data
foreach (DataTable t in tables)
{
table.Merge(t); // same as table.Merge(t, false, MissingSchemaAction.Add);
}
table.EndLoadData();
if (primaryKeyColumn != null)
{
// since we might have no real primary keys defined, the rows now might have repeating fields
// so now we're going to "join" these rows ...
var pkGroups = table.AsEnumerable()
.GroupBy(r => r[primaryKeyColumn]);
var dupGroups = pkGroups.Where(g => g.Count() > 1);
foreach (var grpDup in dupGroups)
{
// use first row and modify it
DataRow firstRow = grpDup.First();
foreach (DataColumn c in table.Columns)
{
if (firstRow.IsNull(c))
{
DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
if (firstNotNullRow != null)
firstRow[c] = firstNotNullRow[c];
}
}
// remove all but first row
var rowsToRemove = grpDup.Skip(1);
foreach (DataRow rowToRemove in rowsToRemove)
table.Rows.Remove(rowToRemove);
}
}
return table;
}
这工作得很好做工会,但我不知道是否有一种更简单的方法来做到这一点已经存在于.NET中,让我做任何种类的联合或联合在一组分离数据表(不只是联合在t他上面的代码)还是我必须自定义代码的每种类型的联合/联合?
我有一种感觉,这是案件,我只是想确保我没有失去一些东西。我会研究使用LINQ方法。谢谢! –