明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

COM組件中如何連接數(shù)據(jù)庫

[摘要]VB- 工程 -引用 - ADO 2.0 ,建一個(gè)類DB,工程名設(shè)為 SQLCONN 以下是偶程序中的部分代碼,大家有空可以試試 Option Explicit Public strError As String Public Conn As New ADODB.Connection Public...

VB- 工程 -引用 - ADO 2.0 ,建一個(gè)類DB,工程名設(shè)為 SQLCONN
以下是偶程序中的部分代碼,大家有空可以試試
Option Explicit
Public strError As String
Public Conn As New ADODB.Connection
Public Rs As New ADODB.Recordset

Public Property Get ErrorString() As String
ErrorString = strError
End Property

Public Function Conn_Open(strSQLServerName, strSQLDBUserName, strSQLDBPassword, strSQLDBName)
Dim strCon As String
Set Conn = New ADODB.Connection
On Error GoTo errHandler:
strCon = "Provider=SQLOLEDB;Server=" & strSQLServerName & ";User ID=" & strSQLDBUserName & ";Password=" & strSQLDBPassword & ";Database=" & strSQLDBName & ";"
Conn.Open strCon
errHandler:
strError = "錯(cuò)誤源:" & Err.Source & vbCrLf & "描述:" & Err.Description
Exit Function
End Function

Function GetRootID(ByVal ClassID)
Dim query
query = "select class_ID,RootID from tblCategory where class_id='" & ClassID & "'"
Set Rs = Conn.Execute(query)
If Not (Rs.EOF And Rs.BOF) Then
GetRootID = Rs("RootID")
Else
GetRootID = ClassID
End If
Rs.Close
Set Rs = Nothing
End Function

編譯后,在ASP中這樣用

function GetRootID(byval id) ' 獲取根類ID號(hào)
set S_DB = server.CreateObject( "SQLCONN.DB")
S_DB.Conn_Open strSQLServerName,strSQLDBUserName,strSQLDBPassword,strSQLDBName
GetRootID = S_DB.GetRootID(id)
Set S_DB=nothing
end function