如何从DB / datagridview / datatable中提取数据

时间:2013-12-11 09:10:24

标签: .net vb.net datagridview datatable

我有这张表..我想提取一个特定的数据..

enter image description here

让我们说..我有Home Depot作为按钮。如何将CameraID的值拉2并将其放在变量上? 像这样......

'dim foo as string = cameraID where MarkerName is Home Depot

1 个答案:

答案 0 :(得分:0)

鉴于您使用DataTable,例如:

' example DataTable '
Dim dt = new DataTable()
dt.Columns.Add("cameraID", gettype(integer))
dt.Columns.Add("MarkerName", gettype(string))
dt.Rows.Add(new object() {2, "Home Depot"})
dt.Rows.Add(new object() {1, "CSI"})

您可以使用LINQ查询:

' foo is now 2 '
Dim foo = dt.AsEnumerable() _
            .Where(function(row) row("MarkerName") = "Home Depot") _
            .Select(function(row) row("cameraID")) _
            .Single()

Select方法来过滤DataTable

' foo2 is now 2 '
Dim foo2 = dt.Select("MarkerName = 'Home Depot'")(0)("cameraID")