将 DataRow 从一个 DataTable 复制到另一个 DataTable?
我有一个非常具体的要求。我有两个数据表(都具有相同的架构),一个表(aqTable)有 2 行,另一个表(rapidsTable)有 5 行。我需要检查 aqTable 中的那 2 行是否存在于 rapidsTable 的那 5 行中,如果这两行存在于 rapidsTable 中,则将剩余(差异)3 行复制到 aqTable。
我以这种方式尝试过,但是当它循环第三行时会抛出异常“位置2没有行”,因为其中一个表中的行数不超过2行。
if (rapidsTable.Rows.Count > aqTable.Rows.Count)
{
try
{
int aqRow = 0; rows = 0;
int rCount = rapidsTable.Rows.Count;
for (int rRow = 0; rRow < rCount; rRow++)
{
if (aqTable.Rows.Count == 0)
{
DataRow row = aqTable.NewRow();
row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
aqTable.Rows.Add(row); rows++;
}
else
{
string str = aqTable.Rows[aqRow].ToString();
if (aqTable.Rows.ToString() == null || aqTable.Rows.ToString() == "")
{ }
if ((aqTable.Rows[aqRow][1].ToString() == rapidsTable.Rows[rRow][1].ToString()) &&
(aqTable.Rows[aqRow][2].ToString() == rapidsTable.Rows[rRow][2].ToString()) &&
(aqTable.Rows[aqRow][3].ToString() == rapidsTable.Rows[rRow][3].ToString()))
{ }
else
{
DataRow row = aqTable.NewRow();
row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
aqTable.Rows.Add(row); rRow = rRow - 1; rows++;
}
}
aqRow++;
}
catch (Exception ex)
{ throw new Exception("R>AQ Copy: " + ex.Message); }
有没有办法解决这个异常。我从许多不同的博客尝试了许多不同的方法。发现这个博客有一些最好的例子,想分享我的问题。
I have a very specific requirement. I've two datatables (both of same schema) one table(aqTable) with 2 rows and another (rapidsTable) with 5 rows. I need to check if those 2 rows in aqTable are present in those 5 rows of rapidsTable or not, if those two rows are present in rapidsTable then copy remaining (difference) 3 rows to aqTable.
I tried it in this way, but when it loops over for 3rd row throws an exception "There is no row a position2" because there are no more than 2 rows in one of the table.
if (rapidsTable.Rows.Count > aqTable.Rows.Count)
{
try
{
int aqRow = 0; rows = 0;
int rCount = rapidsTable.Rows.Count;
for (int rRow = 0; rRow < rCount; rRow++)
{
if (aqTable.Rows.Count == 0)
{
DataRow row = aqTable.NewRow();
row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
aqTable.Rows.Add(row); rows++;
}
else
{
string str = aqTable.Rows[aqRow].ToString();
if (aqTable.Rows.ToString() == null || aqTable.Rows.ToString() == "")
{ }
if ((aqTable.Rows[aqRow][1].ToString() == rapidsTable.Rows[rRow][1].ToString()) &&
(aqTable.Rows[aqRow][2].ToString() == rapidsTable.Rows[rRow][2].ToString()) &&
(aqTable.Rows[aqRow][3].ToString() == rapidsTable.Rows[rRow][3].ToString()))
{ }
else
{
DataRow row = aqTable.NewRow();
row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
aqTable.Rows.Add(row); rRow = rRow - 1; rows++;
}
}
aqRow++;
}
catch (Exception ex)
{ throw new Exception("R>AQ Copy: " + ex.Message); }
Is there a way to solve this for that exception. I tried many different ways from many different blogs. Found this blog has some best examples, thought to share my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这就是您正在寻找的
http://support。 microsoft.com/default.aspx?scid=kb;EN-US;Q308909
I think this is what you are looking for
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308909