下面就舉一個很常用的,大家都很關心的有關如何保密開啟資料庫的過程的例子。
例子很簡單,就是在一個下拉框中顯示資料庫中某個欄位的內容。
整個過程如下:
1。建立一個VB6的ActiveX DLL項目
2。在屬性視窗中,命名你的庫模組和專案檔。例子中為keiths_lookup項目名和lookup模組名.
這寫名字是你在ASP中將引用到的dll函數名(在ASP中的對象名將為keiths_lookup.lookup)。
3.將項目和庫模組使用同樣的名字存檔(當然了,尾碼是不能夠一樣的哦)。
4。從VB6中的項目菜單中選擇References .然後選中Microsoft ActiveX data objects 2.0 library, Microsoft
ActiveX Data Objects Recordset 2.0 library.
檔案的代碼如下:
Public Function html_combo(comboname As String, Lookup_field As String) As String
Dim outstring As String
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sqlstring As String
Set conn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
'下面這些資料庫中的欄位名只是為這個例子設的,你完全可以根據自己的需要修改代碼
Sqlstring = "SELECT [Lookup_Description],[Lookup_Key] FROM _
[Lookup_Table] WHERE [Lookup_Field] = '" & Lookup_Field & "'"
If Not rst.EOF Then
rst.MoveFirst
outstring = "<Select name='" & comboname & "'>"
Do While Not rst.EOF
outstring = outstring & "<Option Value='" & rst.Fields("lookup_key") & "'>" & _
rst.Fields("lookup_description") & "</Option> "
rst.MoveNext
Loop
End If