比较两个数据集在C# [英] Compare two datasets in C#
本文介绍了比较两个数据集在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于Ex:
第二个数据集中的一个数据集ID中的Id
1 1
2 2
3 4
我需要将ID 3插入到第二个数据集。
这是我的代码供您参考:
if(ds.Tables [0] .Rows .Count> 0 || clientDS.Tables [0] .Rows.Count> 0)
{
for(int i = 0; i< ds.Tables [0] .Rows.Count ; i ++)
{
for(int j = 0; j< clientDS.Tables [0] .Rows.Count; j ++)
{
if(ds.Tables [ 0] .Rows [i] [Id] ToString()== clientDS.Tables [0] .Rows [j] [Id]。ToString())
{
客户端。 GetSingleValu e(update customers set Name ='+ ds.Tables [0] .Rows [i] [Name] ToString()+',ContactPerson ='+ ds.Tables [0] .Rows [i ] [ContactPerson] ToString()+',Address ='+ ds.Tables [0] .Rows [i] [Address] ToString()+',TinNo =' Table [0] .Rows [i] [TinNo]。ToString()+',ContactNo ='+ ds.Tables [0] .Rows [i] [Contactno] ToString()+ ,Report ='+ ds.Tables [0] .Rows [i] [Report]。ToString()+',Sync = 0,Ids ='+ ds.Tables [0] .Rows [i] [Id] ToString()+'其中id ='+ ds.Tables [0] .Rows [i] [Id] ToString()+');
}
{
client.GetSingleValue(insert into customers(id,Name,ContactPerson,Address,TinNo,ContactNo,Report,Sync,Ids)values(' + ds.Tables [0] .Rows [i] [Id] ToString()+','+ ds.Tables [0] .Rows [i] [Name] ToString()+ ',''+ ds.Tables [0] .Rows [i] [ContactPerson] ToString()+','+ ds.Tables [0] .Rows [i] [Address] ToString ()+','+ ds.Tables [0] .Rows [i] [TinNo]。ToString()+','+ ds.Tables [0] .Rows [i] [Contactno ] .ToString()+','+ ds.Tables [0] .Rows [i] [Report] ToString()+',0,'+ ds.Tables [0] [i] [Id]。ToString()+'));
}
}
}
}
以上代码不起作用请修正我的问题。
谢谢
解决方案
使用 ==
来调用Id字符串。尝试使用等于
。
我只是使用foreach并选择:
foreach(ds.Tables [0]中的DataRow行) .Rows)
{
string filter = string.Format(Id ={0},row [Id]);
DataRow [] rows = clientDS.Tables [0] .Select(filter);
if(rows.length == 0)
{
// insert here
}
else
{
// update here
}
}
i have two datasets and i need to compare these two datasets such that if ID does not exist in one table then i need to write insert Query else update query.
For Ex:
Id in One dataset ID in second Dataset
1 1
2 2
3 4
I need to insert ID 3 to second dataset.
Here is my code for your reference:
if (ds.Tables[0].Rows.Count > 0 || clientDS.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < clientDS.Tables[0].Rows.Count; j++)
{
if (ds.Tables[0].Rows[i]["Id"].ToString() == clientDS.Tables[0].Rows[j]["Id"].ToString())
{
client.GetSingleValue("update customers set Name='" + ds.Tables[0].Rows[i]["Name"].ToString() + "',ContactPerson= '" + ds.Tables[0].Rows[i]["ContactPerson"].ToString() + "',Address='" + ds.Tables[0].Rows[i]["Address"].ToString() + "',TinNo='" + ds.Tables[0].Rows[i]["TinNo"].ToString() + "',ContactNo='" + ds.Tables[0].Rows[i]["Contactno"].ToString() + "',Report= '" + ds.Tables[0].Rows[i]["Report"].ToString() + "',Sync=0,Ids='" + ds.Tables[0].Rows[i]["Id"].ToString() + "' where id='" + ds.Tables[0].Rows[i]["Id"].ToString() + "' ");
}
else
{
client.GetSingleValue("insert into customers(id,Name,ContactPerson,Address,TinNo,ContactNo,Report,Sync,Ids) values('" + ds.Tables[0].Rows[i]["Id"].ToString() + "', '" + ds.Tables[0].Rows[i]["Name"].ToString() + "','" + ds.Tables[0].Rows[i]["ContactPerson"].ToString() + "', '" + ds.Tables[0].Rows[i]["Address"].ToString() + "', '" + ds.Tables[0].Rows[i]["TinNo"].ToString() + "', '" + ds.Tables[0].Rows[i]["Contactno"].ToString() + "', '" + ds.Tables[0].Rows[i]["Report"].ToString() + "',0,'" + ds.Tables[0].Rows[i]["Id"].ToString() + "')");
}
}
}
}
Above code does not work. Pls rectify my issue.
Thanks
解决方案
I think your error is compaing the Id string using ==
. Try using Equals
.
I'd just use a foreach and select instead:
foreach (DataRow row in ds.Tables[0].Rows)
{
string filter = string.Format("Id = '{0}'", row["Id"]);
DataRow[] rows = clientDS.Tables[0].Select(filter);
if (rows.length == 0)
{
// insert here
}
else
{
// update here
}
}
这篇关于比较两个数据集在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文