DataTable.Select 方法存在问题

发布于 2024-09-07 12:57:52 字数 1209 浏览 1 评论 0原文

我的字符串位于字符串数组中,我的数据位于数据表中。

我要做的是,使用 datatable.select(string) 方法选择一行并将其从数据表中删除。

这就是我正在做的事情,它多次工作正常,但如果我频繁更改数组,它会返回错误。我已经检查了数据表中的数据行以及匹配的字符串。他们就在那里,因为他们应该在那里。但有时它仍然返回 null。但当我检查时,数据完全符合我的要求。

             for (int k = 0; k < p; k++)
                {
                    string tempMapString = "ID =" + IDArray[k];
                  try
                    {
                        DataRow[] rows = myTable_2_ForCBL.Select(tempMapString);

                        //MessageBox.Show(IDArray[k].ToString(), "ID Array Element");
                        MessageBox.Show(tempMapString + "  " + rows.Length.ToString(), "No of Rows mapped are:");



                        if (rows.Length > 0)
                        {
                            foreach (DataRow row in rows)
                            {
                                row.Delete();
                            }
                        }
                    }
                    catch (Exception err2)
                    {
                        MessageBox.Show(err2.Message);
                    }

                }

如果我遗漏了什么,请告诉我。数据表中的列是“ID”和“LocationName”。 ID就像主键一样,是复选框列表的值成员。

I have my strings in a String array, my Data in a datatable.

What I have to do is, select a single row using datatable.select(string) method and delete it from the datatable.

this is what I am doing, and its working fine many times but its returning an error if I change the array frequently. I have checked the datarows in the datatable and also the matching string. they are there as they are supposed to be. But some times it still returns null. But when I checkback, the data is exactly as I want it to be.

             for (int k = 0; k < p; k++)
                {
                    string tempMapString = "ID =" + IDArray[k];
                  try
                    {
                        DataRow[] rows = myTable_2_ForCBL.Select(tempMapString);

                        //MessageBox.Show(IDArray[k].ToString(), "ID Array Element");
                        MessageBox.Show(tempMapString + "  " + rows.Length.ToString(), "No of Rows mapped are:");



                        if (rows.Length > 0)
                        {
                            foreach (DataRow row in rows)
                            {
                                row.Delete();
                            }
                        }
                    }
                    catch (Exception err2)
                    {
                        MessageBox.Show(err2.Message);
                    }

                }

let me know if I am missing something. There columns in the datatable are "ID" and "LocationName". ID is like the primary key and is the valuemember of the checkedbox list.

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

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

发布评论

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

评论(1

混吃等死 2024-09-14 12:57:52

我得到了答案。这是微软在其文档中忽略的一个愚蠢的错误。

您需要为字符串添加单引号 (')。所以该行将是


DataTable1.Select("ID= '"+tempstring+"'");

I got the answer. Its a Stupid BUG which Microsoft ignored in their documentation.

you need to put single quote (') for the string. so the line would be


DataTable1.Select("ID= '"+tempstring+"'");

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