三层结构中一般都是这样的 举个例子 一个dll负责数据库所有连接p.class1 Public Function OpenConnection() As ADODB.Connection Dim Login As String Dim Pwd As String Dim ado As New ADODB.Connection On Error GoTo ErrorHandler
ado.CursorLocation = adUseClient ado.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=MyBookShop;Data Source=yang" ado.Open Set OpenConnection = ado Exit Function ErrorHandler: err.Raise err.Number, "数据库连接", err.Description End Function一个dll负责所有SQL语句操作p.class2 Public Function FindAll(ByVal depth As Integer) As ADODB.Recordset Dim strSql As String On Error GoTo err Dim objCtx As ObjectContext Set objCtx = GetObjectContext() strSql = "select * from CateGory where depth<=" & depth
Dim conn As ADODB.Connection 引用上面的dll dim obj as p.class1 set obj=new p.class1 Set conn = obj.OpenConnection() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset Set rs = conn.Execute(strSql)Set FindAll = rsIf Not conn Is Nothing Then Set conn = Nothing End IfIf Not objCtx Is Nothing Then objCtx.SetComplete End If Exit Functionerr: If Not conn Is Nothing Then Set conn = Nothing End IfIf Not objCtx Is Nothing Then objCtx.SetAbort End If err.Raise err.Number, "CateGoryDataService.CateGoryData.FindAll", err.DescriptionEnd Function
举个例子
一个dll负责数据库所有连接p.class1
Public Function OpenConnection() As ADODB.Connection
Dim Login As String
Dim Pwd As String
Dim ado As New ADODB.Connection
On Error GoTo ErrorHandler
ado.CursorLocation = adUseClient
ado.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=MyBookShop;Data Source=yang"
ado.Open
Set OpenConnection = ado
Exit Function
ErrorHandler:
err.Raise err.Number, "数据库连接", err.Description
End Function一个dll负责所有SQL语句操作p.class2
Public Function FindAll(ByVal depth As Integer) As ADODB.Recordset
Dim strSql As String
On Error GoTo err
Dim objCtx As ObjectContext
Set objCtx = GetObjectContext()
strSql = "select * from CateGory where depth<=" & depth
Dim conn As ADODB.Connection
引用上面的dll
dim obj as p.class1
set obj=new p.class1
Set conn = obj.OpenConnection()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set rs = conn.Execute(strSql)Set FindAll = rsIf Not conn Is Nothing Then
Set conn = Nothing
End IfIf Not objCtx Is Nothing Then
objCtx.SetComplete
End If
Exit Functionerr:
If Not conn Is Nothing Then
Set conn = Nothing
End IfIf Not objCtx Is Nothing Then
objCtx.SetAbort
End If
err.Raise err.Number, "CateGoryDataService.CateGoryData.FindAll", err.DescriptionEnd Function