【问题标题】:get different column value from two tables which has same primary key in c#从C#中具有相同主键的两个表中获取不同的列值
【发布时间】:2015-07-16 07:30:17
【问题描述】:

我有两个具有相同名称/架构但具有不同值的表。 我需要找到具有相同主键(第一列)但值不同的行。 前任。 我的表:

id   name   age
1    ram    25
2    mohan   30

我的表:

id  name  age
3   harry  26
**1   ram   35**
3   tony   45

所以我需要 2 个表中的 2 行,值为 35。 它应该将整行作为数据表或数据行返回。 我正在使用 oracle 数据库。此解决方案需要 c# 代码。 它也应该适用于其他表的多个列值。 我的代码..

public OracleCommand getColumns(OracleConnection connection, DataTable table, int i, string tab, DataTable table3)
{
    int columCount = table.Columns.Count;
    string [] colArray = new string[columCount];
    string pkey = table.Columns[0].ColumnName;
    string pkeyValue = table.Rows[i][0].ToString();
    string query2 = "SELECT * FROM " + tab +
                " WHERE " + tab + "." + pkey + " = '" + pkeyValue + "'";
    OracleCommand command = new OracleCommand();
    int k = 0;
    int  X =0;

    for(int j=1 ; j<colArray.Length;j++)
    {
        string column = table.Columns[j].ColumnName;
        string columnValue = table.Rows[i][j].ToString();
        string add = " OR " + tab + "." + column + " = '" + columnValue + "'";
        query2 += add;
        command.CommandText = query2;
        command.CommandType = CommandType.Text;
        command.Connection = connection;
        var check = command.ExecuteNonQuery();
        if (check == null)
        {
            k++;
        }
        else
            X++;   
    }
     return command;
}

【问题讨论】:

  • 谢谢兄弟。我是新来的……
  • 我不太了解你的目标 - 你能澄清你的问题吗?
  • 所以你想得到一行 [1, ram, 25] 和一行 [1, ram, 35],因为 id=1 匹配,但年龄不匹配?
  • 所以你需要 select id from tab1 t , tab2 w where t.id=w.id ?
  • 我需要得到关注的价值..

标签: c# mysql oracle linq-to-sql


【解决方案1】:

这是您的表格的示例,我正在呈现来自 t2 的数据,这些数据与 t1 中的行不匹配:

using Oracle.DataAccess.Client;

...

public string OraText(string pkey, string[] tables, string[] columns)
{
    string sSQL = "select " + pkey + "";
    foreach (string s in columns)
    {
        sSQL += ", " + tables[1] + "." + s;
    }
    sSQL += Environment.NewLine + "  from t1 join t2 using (" + pkey + ") "
        + Environment.NewLine + "  where 1=0 ";
    foreach (string s in columns)
    {
        sSQL += " or " + tables[0] + "." + s + " <> " + tables[1] + "." + s;
    }

    return sSQL;
}

private void Form1_Load(object sender, EventArgs e)
{
    OracleConnection oc = new OracleConnection(
        "User Id=scott;Password=tiger;Data Source=XE");
    oc.Open();

    string[] tables = {"t1", "t2"};
    string[] columns = {"name", "age"};

    string sSQL = OraText("id", tables, columns);
    OracleCommand oracmd = new OracleCommand(sSQL, oc);
    OracleDataReader reader = oracmd.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(reader.GetValue(0) + " " 
            + reader.GetValue(1) + " " +reader.GetValue(2));
    }

    oc.Close();
}

控制台输出:

1 ram 35

【讨论】:

  • 这两个表都来自不同的数据库...但是名称和结构是相同的值/行会不同。
  • 所以你有三种可能性:1)在第一个数据库中创建database link到第二个数据库并使用这个链接加入数据 - 在这种情况下你可以使用我的解决方案(修改tables类似于{@ 987654325@}) 2) 将两个表中的数据收集到 C# DataTable 对象中,并将此对象逐行与匹配的 PK 进行比较 3) 使用 LINQ 比较 DataTable 对象(我对此不太擅长,但您可以在净)。
  • 如果您有可能创建此解决方案最快的链接,我会这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2012-05-28
  • 2018-10-05
  • 1970-01-01
相关资源
最近更新 更多