Vb.net 在Access数据表中查找行
我需要查看数据表中是否存在一行,其中表中的字段(plan_代码)与变量(strPlanCode)匹配。如果找到匹配项,则从该行的另一个字段获取值;如果没有,则将该行写入错误列表。我开始的方向是建立一个数据表,如下所示:Vb.net 在Access数据表中查找行,vb.net,datatable,find,ms-access-2010,Vb.net,Datatable,Find,Ms Access 2010,我需要查看数据表中是否存在一行,其中表中的字段(plan_代码)与变量(strPlanCode)匹配。如果找到匹配项,则从该行的另一个字段获取值;如果没有,则将该行写入错误列表。我开始的方向是建立一个数据表,如下所示: Using daProduct As New OleDbDataAdapter("SELECT * FROM [product]", con) Dim cbProduct = New OleDbCommandBuilder(daProduct) cbExtract
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)
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
但是那里的方法似乎不起作用,我想知道我是否不应该走DataAdapter/DataTable路径
我尝试过的一些方法是:
strSearch = "plan_code = " & strPlanCode
intProdRow = dtProduct.Select(strSearch)
及
但这些都不会得到结果和/或编译
使用ODBC连接SQL Anywhere数据库的旧代码如下所示:
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)
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