2017-04-03 54 views
0

我目前正在完成一個項目,其中一系列組合框將有望鏈接回單個數據庫記錄,我將採用5個值,並且我具有以下代碼如何輸出記錄它已經返回,所以我可以使用在表中找到的值沒有被用戶輸入,在代碼的後面部分?如何從訪問查詢中返回記錄

myConnection.ConnectionString = My.Forms.Main.connString 
myConnection.Open() 
Dim match As String = "SELECT CBrand, CModel, CSpec,BodyStyle,CNoOfDoors, CEngineSize, COTR, TransmissionCost 
FROM CarFigures WHERE CBrand = cbBrand.text 
CModel = cbModel.text 
CSpec = cbSpec.text 
CNoOfDoors = cbNoOfDoors.text 
EngineSize = cbEngineSize.text" 

回答

0

你打開一個連接並具有查詢後,你需要這樣做:

myConnection.ConnectionString = My.Forms.Main.connString 
myConnection.Open() 
Dim match As String = "SELECT CBrand, CModel, ..." 

' define data adapter, data set, fill data set then loop' 
Dim da As New OleDb.OleDbDataAdapter 
Dim ds As New Dataset 
Dim row As System.Data.DataRow 
Dim count As Integer 
da.SelectCommand = New OleDb.OleDbCommand(match, myConnection) 
da.Fill(ds, "myTable") 
count = ds.Tables("myTable").Rows.Count 

While count > -1 
    row = ds.Tables("myTable").Rows.Item(count) 
    ' this is where you take row.Item(0) and do something with it... ' 
    count = count - 1 
End While 

myConnection.Close() 
+0

謝謝,對不起,我還在學習用vb的基本知識,我只是想知道的...是在第三行,我把我的代碼中的where sql命令在那裏,我是否需要命名錶中的「mytable」? – Courteney

+0

不確定你把哪條線稱爲第三條線;你可以爲你的表命名 – bc004346

+0

如果我的答案幫助你,請考慮upvoting /接受一個答案 – bc004346