C# 在c中,从具有相同主键的两个表中获取不同的列值
我有两个名称/模式相同但值不同的表。 我需要找到主键第1列相同但值不同的行。 前任。 我的桌子:C# 在c中,从具有相同主键的两个表中获取不同的列值,c#,mysql,oracle,linq-to-sql,C#,Mysql,Oracle,Linq To Sql,我有两个名称/模式相同但值不同的表。 我需要找到主键第1列相同但值不同的行。 前任。 我的桌子: id name age 1 ram 25 2 mohan 30 我的桌子: id name age 1 ram 25 2 mohan 30 id name age 3 harry 26 **1 ram 35** 3 tony 45 所以我需要2个表中的2行,值为35。 它应该将整行作为数据表或数据行返回。 我
id name age
1 ram 25
2 mohan 30
我的桌子:
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;
}
下面是您的表格示例,我展示的数据来自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,25]和一行[1,ram,35],因为id=1匹配,但年龄不匹配?所以你需要像从表1 t中选择id这样的东西,tab2 w其中t.id=w.id?我需要获取起始值..两个表都来自不同的数据库…但名称和结构相同,值/行将不同。因此,您有三种可能性:1在第一个数据库中创建第二个数据库,并使用此链接连接数据-在这种情况下,您可以使用我的解决方案将表修改为类似于{t1,t2@dblink}2将两个表中的数据收集到C DataTable对象中,并将此对象与匹配PK的3使用LINQ比较DataTable对象逐行进行比较。我不太擅长这一点,但您可以在网络中搜索示例。如果您有可能创建此解决方案最快的链接,我会这样做。