0

我有两个数据集,我正在从 Access 数据库中读取两个表。我希望能够通过 EXPRESSION 匹配数据,以便保留所有匹配的“SN”并摆脱那些没有匹配的数据。到目前为止,我的代码保留了所有的“SN”。这里是,

con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+filePath);
con3 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+filePath);

if ((string)comboBox1.SelectedItem == "CGA0112")
{
    try
    {
         string end = "ENDDATE";
         string qual = "CGA0112";
         string start = "START";

         ad.SelectCommand = new OleDbCommand("SELECT b.RSV_CAT, b.SEQNUM, b.LEASE,
                             b.WELL_ID, a.QUALIFIER, a.KEYWORD, a.EXPRESSION 
                            FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b
                              on a.PROPNUM=b.PROPNUM WHERE a.KEYWORD = '" +end+ "' 
                   AND a.QUALIFIER = '" +qual+ "' AND a.EXPRESSION LIKE 'SN%'", con); 

       ds.Clear();
       ad.Fill(ds);

       con.Open();
       ad.SelectCommand.ExecuteNonQuery();
       con.Close();

       ad3.SelectCommand = new OleDbCommand("SELECT b.RSV_CAT, b.SEQNUM, b.LEASE, 
                               b.WELL_ID, a.QUALIFIER, a.KEYWORD, a.EXPRESSION 
                               FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b 
                              on a.PROPNUM=b.PROPNUM 
                              WHERE a.KEYWORD = '" + start + "' AND a.QUALIFIER = '" + 
                               qual + "' AND a.EXPRESSION LIKE 'SN%'", con3);
       ds3.Clear();
       ad3.Fill(ds3);

       ds.Merge(ds3); //merges the two datasets together

       win1.frm1 = this;
       win1.Show();

       con3.Open();
       ad3.SelectCommand.ExecuteNonQuery();
       con3.Close();
     }

我想知道是否有可能的解决方案,所以它只保留匹配的表达式。任何帮助深表感谢。谢谢。

4

2 回答 2

0

这是一个例子。由于“SN456”出现在两个表中,因此在结果中被选中。

        var ds = new DataSet();
        var table1 = ds.Tables.Add("Table1");
        table1.Columns.Add("Expression", typeof (string));
        table1.Rows.Add("SN123");
        table1.Rows.Add("SN456");
        table1.Rows.Add("SN789");
        var table2 = ds.Tables.Add("Table2");
        table2.Columns.Add("Expression", typeof (string));
        table2.Rows.Add("SN000");
        table2.Rows.Add("SN456");
        table2.Rows.Add("SN999");

        var table1Rows = table1.AsEnumerable();
        var table2Rows = table2.AsEnumerable();

        var matches = table1Rows.Join(table2Rows, l => l["Expression"], r => r["Expression"], (l, r) => l["Expression"]);
        foreach (var match in matches)
        {
            Console.WriteLine(match);
        }
于 2013-07-24T18:04:52.093 回答
0

您是否要获取两个表中的所有 SN?如果是这样,您应该尝试像这样的子查询。

    "SELECT b.RSV_CAT, b.SEQNUM, b.LEASE,
    b.WELL_ID, a.QUALIFIER, a.KEYWORD, a.EXPRESSION 
    FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b
    on a.PROPNUM=b.PROPNUM WHERE a.KEYWORD = '" +end+ "' 
    AND a.QUALIFIER = '" +qual+ "' AND a.EXPRESSION IN 
   (SELECT a.EXPRESSION FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b on 
    a.PROPNUM=b.PROPNUM WHERE a.KEYWORD = '" + start + "' AND a.QUALIFIER = '" + qual  
    + "' AND  a.EXPRESSION LIKE 'SN%'", con3);

这应该给你那些匹配。此代码将为您提供所有 SN,它们都是具有两个不同关键字的表。

于 2013-07-24T17:53:23.840 回答