想写一个ado类,不知类的初始化代码如何写?即在类初始化时,要将"连接串"的信息传进来.
-------------------
以下是代码,功能已经实现了,可是我想在类对象初始化时,就会自动建立连接,而不是执行一个类中的函数再建连接,可是类初始化如何写呢?
Option Explicit
'---------------------------------------------
'类名:clsado
'创建:2006/06/05
'更新:2006/07/11
'---------------------------------------------
Private strlink As String '连接串
Private cn As New ADODB.ConnectionPublic Function linkdatabase() As Boolean '连接数据库,正常返回真
'如果不处于连接状态,则进行连接 On Error GoTo gherr
If cn.State <> adStateOpen Then
With cn
.ConnectionString = strlink
.ConnectionTimeout = 60
.Open '连接
End With
End If
If cn.State = adStateOpen Then
linkdatabase = True
Else
linkdatabase = False
End If
Exit Function
gherr:
linkdatabase = False
MsgBox Err.Description
End Function
'----------------------------------
'接收传来的连接串
'----------------------------------
Public Property Let ConnectionString(strlink1 As String)
strlink = strlink1
End Property
'-------------------------------
'执行一个sql语句,得到一个记录集
'名称:OpenSQL
'功能:打开SQL语句
'输入:vSQL:要打开的SQL语句
'输出:
'返回:成功:记录集(需要调用者释放);失败:空
'-------------------------------
Public Function OpenSQL(Optional ByVal vSQL As String, _
Optional ByVal vCursorType As ADODB.CursorTypeEnum, _
Optional ByVal vLockType As ADODB.LockTypeEnum = adLockBatchOptimistic, _
Optional ByVal vKey As String) As ADODB.Recordset
On Error GoTo ErrorHandle
Dim strsql As String
Dim rs As New ADODB.Recordsetstrsql = Trim(vSQL)
If strsql = "" Then
MsgBox "查询字符串不能为空!", 32, "提示"
Exit Function
End IfIf cn.State <> adStateOpen Then
GoTo ErrorHandle
End If
'使用客户端游标
rs.CursorLocation = adUseClientWith rs
.ActiveConnection = cn
.CursorType = vCursorType
.LockType = vLockType
.Open strsql
End WithSet OpenSQL = rsExit FunctionErrorHandle:
MsgBox "class error"
If Err.Number <> 0 Then
MsgBox Err.Description
End If
Set OpenSQL = Nothing
End Function
'--------------------------------------------------
'名称:ExecSQL
'功能:执行SQL语句
'输入:aStrSQL:执行的SQL语句
'输出:
'返回:TRUE:成功;FALSE:失败
'--------------------------------------------------
Public Function ExecSQL(aStrSQL As String) As Boolean
On Error GoTo errhandle If cn.State <> adStateOpen Then
GoTo errhandle
End If
If linkdatabase = True Then
cn.Execute aStrSQL
ExecSQL = True
Else
ExecSQL = False
End If
Exit Function
errhandle:
MsgBox "数据更新没有成功,可能与服务器连接失败![原因未知]", 32, "警告"
ExecSQL = False
End Function
'--------------------------------------------------
'名称:GetConn
'功能:取得数据库连接[让外面可以使用本类连接对象,如果将本类实例成全局对象,则外面可以使用本类的连接]
'输入:
'输出:
'返回:成功:数据库连接;失败:返回空
'--------------------------------------------------
Public Property Get dbConn() As ADODB.Connection
On Error GoTo errhandle
If linkdatabase = True Then
Set dbConn = cn
Else
Set dbConn = Nothing
End If
Exit Property
errhandle:
Set dbConn = Nothing
End Property'类初始化
Private Sub Class_Initialize()
=====关键就是这里的代码如何写,比如在ado类对象在创建实例时,自动生成内部的连接对象,
请教,各位老师了.
End Sub'释放类
Private Sub Class_Terminate()
End Sub
-------------------
以下是代码,功能已经实现了,可是我想在类对象初始化时,就会自动建立连接,而不是执行一个类中的函数再建连接,可是类初始化如何写呢?
Option Explicit
'---------------------------------------------
'类名:clsado
'创建:2006/06/05
'更新:2006/07/11
'---------------------------------------------
Private strlink As String '连接串
Private cn As New ADODB.ConnectionPublic Function linkdatabase() As Boolean '连接数据库,正常返回真
'如果不处于连接状态,则进行连接 On Error GoTo gherr
If cn.State <> adStateOpen Then
With cn
.ConnectionString = strlink
.ConnectionTimeout = 60
.Open '连接
End With
End If
If cn.State = adStateOpen Then
linkdatabase = True
Else
linkdatabase = False
End If
Exit Function
gherr:
linkdatabase = False
MsgBox Err.Description
End Function
'----------------------------------
'接收传来的连接串
'----------------------------------
Public Property Let ConnectionString(strlink1 As String)
strlink = strlink1
End Property
'-------------------------------
'执行一个sql语句,得到一个记录集
'名称:OpenSQL
'功能:打开SQL语句
'输入:vSQL:要打开的SQL语句
'输出:
'返回:成功:记录集(需要调用者释放);失败:空
'-------------------------------
Public Function OpenSQL(Optional ByVal vSQL As String, _
Optional ByVal vCursorType As ADODB.CursorTypeEnum, _
Optional ByVal vLockType As ADODB.LockTypeEnum = adLockBatchOptimistic, _
Optional ByVal vKey As String) As ADODB.Recordset
On Error GoTo ErrorHandle
Dim strsql As String
Dim rs As New ADODB.Recordsetstrsql = Trim(vSQL)
If strsql = "" Then
MsgBox "查询字符串不能为空!", 32, "提示"
Exit Function
End IfIf cn.State <> adStateOpen Then
GoTo ErrorHandle
End If
'使用客户端游标
rs.CursorLocation = adUseClientWith rs
.ActiveConnection = cn
.CursorType = vCursorType
.LockType = vLockType
.Open strsql
End WithSet OpenSQL = rsExit FunctionErrorHandle:
MsgBox "class error"
If Err.Number <> 0 Then
MsgBox Err.Description
End If
Set OpenSQL = Nothing
End Function
'--------------------------------------------------
'名称:ExecSQL
'功能:执行SQL语句
'输入:aStrSQL:执行的SQL语句
'输出:
'返回:TRUE:成功;FALSE:失败
'--------------------------------------------------
Public Function ExecSQL(aStrSQL As String) As Boolean
On Error GoTo errhandle If cn.State <> adStateOpen Then
GoTo errhandle
End If
If linkdatabase = True Then
cn.Execute aStrSQL
ExecSQL = True
Else
ExecSQL = False
End If
Exit Function
errhandle:
MsgBox "数据更新没有成功,可能与服务器连接失败![原因未知]", 32, "警告"
ExecSQL = False
End Function
'--------------------------------------------------
'名称:GetConn
'功能:取得数据库连接[让外面可以使用本类连接对象,如果将本类实例成全局对象,则外面可以使用本类的连接]
'输入:
'输出:
'返回:成功:数据库连接;失败:返回空
'--------------------------------------------------
Public Property Get dbConn() As ADODB.Connection
On Error GoTo errhandle
If linkdatabase = True Then
Set dbConn = cn
Else
Set dbConn = Nothing
End If
Exit Property
errhandle:
Set dbConn = Nothing
End Property'类初始化
Private Sub Class_Initialize()
=====关键就是这里的代码如何写,比如在ado类对象在创建实例时,自动生成内部的连接对象,
请教,各位老师了.
End Sub'释放类
Private Sub Class_Terminate()
End Sub
----------------------
作用就是:让这个类对象实例化时,一经实例化,就会自动创建一个有效连接呀!
我的理解不知对否?思路不知对否?
请指教.