简体   繁体   English

在C#中合并数据表

[英]Merging datatables in C#

I have 2 identically constructed datatables. 我有2个构造相同的数据表。 The difference is where they get their data. 不同之处在于他们从何处获取数据。

public DataTable dtPubSearchResultsFromTable(string sqlQuery)
{
    // Returns a datatable of publication search results based on data in table.
    SqlConnection con = new SqlConnection(getConnectionString());
    SqlCommand cmd = new SqlCommand(sqlQuery, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    dt.Columns.Add("Pages", typeof(string));
    da.Fill(dt);
    dt.PrimaryKey = new DataColumn[] { dt.Columns["publicationID"] };

    return dt;
}

public DataTable dtPubSearchResultsFromFiles(string sqlQuery, string safeKeyword)
{
    // Returns a datatable of publication search results based on PDF files.
    SqlConnection con = new SqlConnection(getConnectionString());
    SqlCommand cmd = new SqlCommand(sqlQuery, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    dt.Columns.Add("Pages", typeof(string));
    da.Fill(dt);
    dt.PrimaryKey = new DataColumn[] { dt.Columns["publicationID"] };

    return dt;
}

Since the second datatable is looking through PDF file for specific keywords, I populate the "Pages" column. 由于第二个数据表正在通过PDF文件查找特定的关键字,因此将填充“页面”列。 Then, I merge the 2 datatables EXPECTING to get a list of documents where the primary key "publicationID" is only listed once and the "Pages" column is updated with the list of page numbers from the 2nd datatable. 然后,我合并2个数据表EXPECTING以获得文档列表,其中主键“ publicationID”仅列出一次,并且“ Pages”列用第二个数据表中的页码列表更新。 (I found this process in another question from 2011) (我在2011年的另一个问题中发现了这个过程)

    var dtC = new DataTable("CombinationOfBoth");
    dtC.Columns.Add("publicationID",typeof(int));
    dtC.Columns.Add("PrimaryAuthor", typeof (string));
    dtC.Columns.Add("title", typeof (string));
    dtC.Columns.Add("rstiNumber", typeof(string));
    dtC.Columns.Add("linkToPublicationPDF", typeof(string));
    dtC.Columns.Add("publishDate", typeof(string));
    dtC.Columns.Add("approvalDate", typeof (string));
    dtC.Columns.Add("Pages", typeof(string));

    dtC.Merge(dt1,false,MissingSchemaAction.Ignore);
    dtC.Merge(dt2, false, MissingSchemaAction.Ignore);

What I ACTUALLY get is a list of all of the document records and the ones that are in BOTH datatables are listed TWICE (once without page numbers, once with page numbers). 我实际上得到的是所有文档记录的列表,并且两个数据表中的记录都被两次列出(一次没有页码,一次是页码)。

It seems I might be using the "MissingSchemaAction" improperly. 看来我可能未正确使用“ MissingSchemaAction”。

Thanks, Bob 谢谢,鲍勃

在黑暗中刺刺-在您原来的2个表中,您已经定义了主键,但是在组合的表中却没有?

As it turns out, I WAS using MissingSchemaAction improperly. 事实证明,我不正确地使用MissingSchemaAction。 It should have been MissingSchemaAction.Add 应该是MissingSchemaAction.Add

Adding a primary key to the data table that merges the dt1 and dt2 should solve that issue. 向合并了dt1和dt2的数据表添加主键应该可以解决该问题。 Add the following line before merging: 合并前添加以下行:

    dtC.PrimaryKey = new DataColumn[] { dtC.Columns["publicationID"] };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM