DATABASE 和 DATATABLE 之间的重复检查 [英] Duplicacy check between DATABASE and DATATABLE
问题描述
我的数据库中有一个名为 PNUMSET(主键)的列包含唯一数据.(大约 1L 行)
i have one column in my database by name PNUMSET (Primary Key) contains unique data.(approx 1L rows)
在应用程序中,我有一个包含数据的列名 NEWPNUM 的数据表.
and in application i have one datatable with one column name NEWPNUM which contains data.
我想检查现有数据库和当前数据表值之间是否没有匹配的值..
i want to check that no value is matching between existing database and current datatable values..
注意:- 数据库和数据表中的行数可能相同也可能不同.
Note:- no of rows may or may not be same in database and datatable.
到目前为止我尝试过.....
so far i tried.....
String query = "Select PNUMSET FROM DUMMYTABLE";
MySqlDataAdapter msda = new MySqlDataAdapter(query, connection);
msda.Fill(dt);
for (int k = 0; k < Class1.global_dataset.Tables[0].Rows.Count; k++)
{
if (dt.Rows.Contains(Class1.global_dataset.Tables[0].Rows[k][4].ToString()))
{
MessageBox.Show("Baj Gaya Ghanta!!!!");
}
}
推荐答案
您可以使用 Linq-To-DataTable
来加入这列上的两个表,例如:
You can use Linq-To-DataTable
to join both tables on this column, for example:
var commonRows = from r1 in dt.AsEnumerable()
join r2 in Class1.global_dataset.Tables[0].AsEnumerable()
on r1.Field<int>(4) equals r2.Field<int>(4)
select r1;
if(commonRows.Any())
{
// do something with these rows
}
(假设第 5 列是 int
)
(assuming the 5th column and it's type int
)
请注意,虽然 Enumerable.Join
非常有效,最好在数据库中进行比较,而不是将所有内容加载到内存中.
Note that although Enumerable.Join
is quite efficient it might be better to compare this in the database instead of loading all into memory.
这篇关于DATABASE 和 DATATABLE 之间的重复检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!