簡體   English   中英

在訪問數據表中查找行

[英]Find Row in Access DataTable

我需要查看DataTable中是否存在一行,其中表(plan_code)中的字段與變量(strPlanCode)相匹配。 如果找到匹配項,則從該行中的另一個字段獲取值; 如果不是,它將把該行寫入錯誤列表。 我開始的方向是建立像這樣的DataTable:

Using daProduct As New OleDbDataAdapter("SELECT * FROM [product]", con)
    Dim cbProduct = New OleDbCommandBuilder(daProduct)
    cbExtract.QuotePrefix = "["
    cbExtract.QuoteSuffix = "]"
    Dim dtProduct = New DataTable
    daProduct.Fill(dtProduct)

但是那里的任何方法似乎都行不通,我想知道是否不應該沿DataAdapter / DataTable路徑訪問。

我嘗試過的一些方法是:

strSearch = "plan_code = " & strPlanCode
intProdRow = dtProduct.Select(strSearch)

intProdRow = dtProduct.SelectCommand(strSearch) 

但是這些都不會有結果和/或可以編譯。

使用ODBC連接到SQL Anywhere數據庫的舊代碼如下所示:

ls_command = "select * from product"
selectCMD = New OdbcCommand(ls_command, connectDB) 
selectCMD.CommandTimeout = 30 
productDA.SelectCommand = selectCMD
productDA.Fill(clp_valDS, "product")
porductTBL = clp_valDS.Tables("product") 
productTBL.PrimaryKey = New DataColumn() {productTBL.Columns("plan_code")}

productDR = productTBL.Rows.Find(ls_plan_code)
If (productDR Is Nothing) Then
    ls_error_message = "Plan Code " + ls_plan_code + " not found"
    GoTo ErrorHandler
Else
    ls_secondary_guar = productDR("secondary_guar")
End If

我將使用以下方法:

For Each row As System.Data.DataRow In dtProduct.Rows
   if not row.item("plan_code") is DBNull.Value then
      If row.Item("plan_code").ToString = strPlanCode Then
         'do what you want with the matching row.
      End If
   end if
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM