DataTable.Select 方法存在问题
我的字符串位于字符串数组中,我的数据位于数据表中。
我要做的是,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到了答案。这是微软在其文档中忽略的一个愚蠢的错误。
您需要为字符串添加单引号 (')。所以该行将是
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+"'");