一個(gè)VB 的優(yōu)化案例 關(guān)勁松 PMP 關(guān)于測(cè)試軟件,ComboBox讀取數(shù)據(jù)庫(kù)生成下拉列表的算法簡(jiǎn)化。 原有代碼: 要遍歷整個(gè)表的數(shù)據(jù)。使用了兩次嵌套循環(huán),加if判斷,加標(biāo)志位處理。而且代碼繁復(fù),不易理解維護(hù)。 Combo1(1).Clear Combo1(1).AddItem 6, 0 strSelect
一個(gè)VB 的優(yōu)化案例
關(guān)勁松 PMP
關(guān)于測(cè)試軟件,ComboBox讀取數(shù)據(jù)庫(kù)生成下拉列表的算法簡(jiǎn)化。
原有代碼:
要遍歷整個(gè)表的數(shù)據(jù)。使用了兩次嵌套循環(huán),加if判斷,加標(biāo)志位處理。而且代碼繁復(fù),不易理解維護(hù)。
Combo1(1).Clear
Combo1(1).AddItem "6", 0
strSelect = "select * from Code_21 ORDER BY TX_MIN_FREQ_min asc "
Set rs = daev.sys.Execute(strSelect)
if rs.RecordCount > 0 Then
exist_flag = False
rs.MoveFirst
For i = 1 To rs.RecordCount
For j = 0 To Combo1(1).ListCount - 1
If Combo1(1).List(j) = rs.Fields("frequencyband") Then
exist_flag = True
End If
Next j
If exist_flag = False Then
Combo1(1).AddItem rs.Fields("frequencyband"), Combo1(1).ListCount
ElseIf exist_flag = True Then
exist_flag = False
End If
rs.MoveNext
Next i
End If
Combo1(1).Text = Combo1(1).List(0)
改進(jìn)代碼:
關(guān)鍵使用SQL中distinct 僅顯示不相同的記錄字段,同時(shí)用Clng將數(shù)據(jù)庫(kù)定義為文本的frequencyband字段,類(lèi)型轉(zhuǎn)換為長(zhǎng)整形,這樣distinct后就可以自動(dòng)排序。之后的代碼就可以簡(jiǎn)化為一次循環(huán),遍歷返回的數(shù)據(jù)集即可。而且返回的數(shù)據(jù)集較小。
Access的別名定義也要留意,不能與當(dāng)前表的字段重名。因此需要將frequencyband定義為frequency。
strSelect = "select distinct CLng(frequencyband) as frequency from Code_21 "
Set rs = daev.sys.Execute(strSelect)
Combo1(1).Clear
If rs.RecordCount > 0 Then
rs.MoveFirst
For i = 1 To rs.RecordCount
Combo1(1).AddItem rs.Fields("frequency"), Combo1(1).ListCount
rs.MoveNext
Next i
End If
Combo1(1).Text = Combo1(1).List(0)
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com