且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在数据表中查找具有特定 id 的行

更新时间:2023-01-17 18:01:48

制作一个字符串搜索条件,像这样:

Make a string criteria to search for, like this:

string searchExpression = "ID = 5"

然后使用DataTable对象的.Select()方法,像这样:

Then use the .Select() method of the DataTable object, like this:

DataRow[] foundRows = YourDataTable.Select(searchExpression);

现在您可以遍历结果,如下所示:

Now you can loop through the results, like this:

int numberOfCalls;
bool result;
foreach(DataRow dr in foundRows)
{
    // Get value of Calls here
    result = Int32.TryParse(dr["Calls"], out numberOfCalls);

    // Optionally, you can check the result of the attempted try parse here
    // and do something if you wish
    if(result)
    {
        // Try parse to 32-bit integer worked

    }
    else
    {
        // Try parse to 32-bit integer failed

    }
}