在 C# 中从数据集中删除行时出现问题

发布于 2024-09-18 00:21:54 字数 975 浏览 7 评论 0原文

我遇到这种情况:我必须创建一个数据集,该数据集从另一个数据集中获取所有数据。在某些情况下,我必须从每个新数据集中删除一些行。当两个 foreach 循环完成时,两个数据集都是空的,即使只完成第一个,第一个数据集也有一些行。

知道为什么,...或者告诉我哪里有代码错误。

提前谢谢。

//Dataset comes as a parameter.    
DataSet dsFirst = ds;
DataSet dsSecond = ds;

foreach (DataRow dr in dsFirst.Tables[0].Select())
{
    int i = Convert.ToInt32(dr["ROWNUMBER"]);
    //merren vetem veprimet ATM qe jane bere para CoB
    if (i > x && i < y)
        dr.Delete();
    else
        dr["NOVATRANSAKCIJA"] = 0;//eshte derguar njehere, per rrjedhoje nuk do te dergohet sms
}
dsFirst.AcceptChanges();

foreach (DataRow dr1 in dsSecond.Tables[0].Select())
{
    int i = Convert.ToInt32(dr1["ROWNUMBER"]);
    //merren veprimet e sistemit dhe veprimet ATM te bera gjate CoB
    if (i < x || i > y)
        dr1.Delete();
    else
        dr1["NOVATRANSAKCIJA"] = 1;//nuk eshte derguar asnjehere, per rrjedhoje do te dergohet sms

}
dsSecond.AcceptChanges();

I have this situation: I have to create a dataset that takes all data from another dataset. In some cases I have to remove some rows from each of the new dataset. When both foreach cycles are finished, both datasets are empty, even when finish only the first, the first dataset has some rows.

Any idea why, ... or tell where I have code wrong.

Thnx in advance.

//Dataset comes as a parameter.    
DataSet dsFirst = ds;
DataSet dsSecond = ds;

foreach (DataRow dr in dsFirst.Tables[0].Select())
{
    int i = Convert.ToInt32(dr["ROWNUMBER"]);
    //merren vetem veprimet ATM qe jane bere para CoB
    if (i > x && i < y)
        dr.Delete();
    else
        dr["NOVATRANSAKCIJA"] = 0;//eshte derguar njehere, per rrjedhoje nuk do te dergohet sms
}
dsFirst.AcceptChanges();

foreach (DataRow dr1 in dsSecond.Tables[0].Select())
{
    int i = Convert.ToInt32(dr1["ROWNUMBER"]);
    //merren veprimet e sistemit dhe veprimet ATM te bera gjate CoB
    if (i < x || i > y)
        dr1.Delete();
    else
        dr1["NOVATRANSAKCIJA"] = 1;//nuk eshte derguar asnjehere, per rrjedhoje do te dergohet sms

}
dsSecond.AcceptChanges();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除会话 2024-09-25 00:21:54

正如 Marc 已经指出的那样,您在这里使用引用类型值,因此您实际上是使用 ds、dsFirst 和 dsSecond 指向同一数据集。您必须复制数据集才能实现您想要做的事情。

尝试

dsFirst = ds.Copy(); 
dsSecond = ds.Copy();

相反,这将克隆结构并复制数据。

As Marc has already pointed out you are working here with reference type values so you are actually pointing with ds, dsFirst and dsSecond to the same dataset. You have to make a copy of your dataset to achieve what you want to do.

try

dsFirst = ds.Copy(); 
dsSecond = ds.Copy();

instead, which will clone the structure and copy the data.

随梦而飞# 2024-09-25 00:21:54

您只有一个数据集(来自您的代码):

DataSet dsFirst = ds;
DataSet dsSecond = ds;

复制引用不会克隆数据 - 它只是对相同数据集的另一个引用。所以您只需删除所有数据即可。您可以查看 Clone() 方法(但请注意,复制结构,而不是数据)。

You only have one data-set (from your code):

DataSet dsFirst = ds;
DataSet dsSecond = ds;

Copying the reference doesn't clone the data - it is just another reference to the same data-set. So you have simply removed all the data. You might look at the Clone() method (but note that copies the structure, but not the data).

土豪 2024-09-25 00:21:54

正如您现在的代码所示,dsFirstdsSecond 不是原始数据集的副本,而是“指向”同一个数据集!

要复制数据集并随时删除行,我将执行以下操作:

foreach (DataRow dr in ds.Tables[0].Rows)
{
    int i = Convert.ToInt32(dr["ROWNUMBER"]);
    if (i <= x || i >= y)
      dsFirst.Tables[0].ImportRow(row);
}

dsFirst 必须是一个数据集,其中一个表的列与 完全相同>ds。这仅将那些符合不被删除条件的行导入到 dsFirst 中。

您也许可以使用 ds.Clone() 创建包含相应表和列定义的 ds 副本,但我还没有尝试过。使用类型化数据集可以为您节省大量工作,因为您可以根据需要创建任意数量的实例,并且所有表和列都已定义。

As your code is now, dsFirst and dsSecond are not copies of the original dataset, but "point" to the same dataset!

To copy a dataset and remove the rows on the go, I'd do the following:

foreach (DataRow dr in ds.Tables[0].Rows)
{
    int i = Convert.ToInt32(dr["ROWNUMBER"]);
    if (i <= x || i >= y)
      dsFirst.Tables[0].ImportRow(row);
}

dsFirst must be a new dataset with one table with exactly the same columns as ds. This imports only those rows into dsFirst that meet the criteria for not being deleted.

You may be able to create a copy of ds that contains the respective tables and column definitions by using ds.Clone(), but I haven't tried that yet. Working with typed datasets saves you a lot of work here, as you can create as many instances as you want and all tables and columns are already defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文