2

我有一个关于在 Visual Basic 中使用数据库的基本问题。我正在使用 OleDb 连接。我已经从 DataDource 视图中拖放了编辑框。这会自动将表格导航栏放置在表单上。当我运行它时,它工作正常。但是我希望能够使用 SQL 语句在表中进行搜索。如何将 SQL 查询的结果连接到导航栏,以便编辑框自动获取记录的值,而无需手动分配每个文本框?

非常感谢。

4

1 回答 1

0

This little snippet seems to work, although you have to specify the Sort columns for your table, and if you want to sort by multiple fields then on the Find call you pass an array of Object() types that correspond to the values you are searching for (in the order of the Sort values. Let me know if it works for you or if you have any other questions about it.

'**** Sample table structure for Database1Dataset.Table1
'   Col1        Col2        Col3
'(row)  "Row1.Col1" "Row1.Col2" "Row1.Col3"
'(row)  "Row2.Col1" "Row2.Col2" "Row2.Col3"
'(row)  "Row3.Col1" "Row3.Col2" "Row3.Col3"

Dim dv As DataView = Me.Database1DataSet.DefaultViewManager.CreateDataView(Database1DataSet.Table1)
dv.Sort = "Col1"

Me.Table1BindingNavigator.BindingSource.Position = dv.Find("Row2.Col1")

Here is an example with multiple sort columns

Dim dv As DataView = Me.Database1DataSet1.DefaultViewManager.CreateDataView(Database1DataSet1.Table1)
Dim FindValues(1) As Object

dv.Sort = "Col1,Col3"

FindValues(0) = "Row2.Col1"
FindValues(1) = "Row2.Col3"

Me.Table1BindingNavigator.BindingSource.Position = dv.Find(FindValues)
于 2011-11-21T20:28:38.500 回答