主题:类模块中创建连接
对应上面的同类问题(类模块中创建连接)
Option Explicit
Private mvarDataName As String '登陆用户名
Private mvarDataPass As String '登陆密码
Private mvarDataIP As String ' 连接数据库的IP地址
Private mvarDataCatalog As String '要连接的数据库名称。
Private Sub Class_initialize()
' MsgBox "建立新的连接时,先进行初始化"
Set GACN = New ADODB.Connection
End Sub
Private Sub Class_terminate()
If CloseConnect() = True Then
mvarDataName = Empty '登陆用户名‘sa’
mvarDataPass = Empty '登陆密码 'wkl1973'
mvarDataIP = Empty ' 连接数据库的IP地址
mvarDataCatalog = Empty '要连接的数据库名称
End If
End Sub
Public Function CloseConnect() As Boolean
On Error Resume Next
If GACN.State = adStateOpen Then
MsgBox "回收内存", vbExclamation, "终止程序"
GACN.Close
Set GACN = Nothing
CloseConnect = True
End If
End Function
'参数;Catalog 是要连接的数据库名称
'参数;DName 是登陆用户名称。
'参数;DaIp 是要连接的数据库的IP地址。服务器地址
'参数;PsWord 是数据库登陆密码
'功能;连接指定的数据库
Public Function Connection(Catalog As String, _
DName As String, _
DaIp As String, _
PsWord As String) As Boolean
On Error GoTo NOERROR
Dim SQLstring As String
If Catalog = Empty Then
MsgBox "连接的数据库名称不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If DName = Empty Then
MsgBox "登陆用户名称不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If DaIp = Empty Then
MsgBox "要连接的数据库的IP地址不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If PsWord = Empty Then
MsgBox "数据库登陆密码不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If App.PrevInstance = True Then
MsgBox "程序已经起动", vbExclamation, "提示"
Connection = False
Exit Function
End If
SQLstring = "Provider=SQLOLEDB.1;Password=" & PsWord & " ;" & _
"Persist Security Info=False;" & _
"User ID=" & DName & ";" & _
"Initial Catalog=" & Catalog & ";" & _
"Data Source=" & DaIp
GACN.ConnectionTimeout = 30
GACN.Open SQLstring
Connection = True
Exit Function
NOERROR:
Select Case Err.Number
Case -2147467259
MsgBox "您输入的服务器地址有误,请重新输入一个正确的地址", vbExclamation, "地址错误"
Load FrmConnection: FrmConnection.Show
Connection = False
Exit Function
Case -2147217843
MsgBox "服务器登陆用户名填写错误,请输入一个正确的用户名", vbExclamation, "用户名错误"
Load FrmConnection: FrmConnection.Show
Connection = False
Exit Function
Case Else
MsgBox "连接:错误代码:" & Err.Number & " 错误描述:" & Err.Description
Load FrmConnection: FrmConnection.Show
Connection = False
End Select
End Function
Public Property Let DataCatalog(ByVal vData As String)
'是要连接的数据库名称
mvarDataCatalog = vData
End Property
Public Property Get DataCatalog() As String
' 是要连接的数据库名称
DataCatalog = mvarDataCatalog
End Property
Public Property Let DataIP(ByVal vData As String)
'是要连接的数据库的IP地址。
mvarDataIP = vData
End Property
Public Property Get DataIP() As String
'是要连接的数据库的IP地址。
DataIP = mvarDataIP
End Property
Public Property Let DataPass(ByVal vData As String)
'是数据库登陆密码
mvarDataPass = vData
End Property
Public Property Get DataPass() As String
'是数据库登陆密码
DataPass = mvarDataPass
End Property
Public Property Let DataName(ByVal vData As String)
'是登陆用户名称。
mvarDataName = vData
End Property
Public Property Get DataName() As String
'是登陆用户名称。
DataName = mvarDataName
End Property
Option Explicit
Private mvarDataName As String '登陆用户名
Private mvarDataPass As String '登陆密码
Private mvarDataIP As String ' 连接数据库的IP地址
Private mvarDataCatalog As String '要连接的数据库名称。
Private Sub Class_initialize()
' MsgBox "建立新的连接时,先进行初始化"
Set GACN = New ADODB.Connection
End Sub
Private Sub Class_terminate()
If CloseConnect() = True Then
mvarDataName = Empty '登陆用户名‘sa’
mvarDataPass = Empty '登陆密码 'wkl1973'
mvarDataIP = Empty ' 连接数据库的IP地址
mvarDataCatalog = Empty '要连接的数据库名称
End If
End Sub
Public Function CloseConnect() As Boolean
On Error Resume Next
If GACN.State = adStateOpen Then
MsgBox "回收内存", vbExclamation, "终止程序"
GACN.Close
Set GACN = Nothing
CloseConnect = True
End If
End Function
'参数;Catalog 是要连接的数据库名称
'参数;DName 是登陆用户名称。
'参数;DaIp 是要连接的数据库的IP地址。服务器地址
'参数;PsWord 是数据库登陆密码
'功能;连接指定的数据库
Public Function Connection(Catalog As String, _
DName As String, _
DaIp As String, _
PsWord As String) As Boolean
On Error GoTo NOERROR
Dim SQLstring As String
If Catalog = Empty Then
MsgBox "连接的数据库名称不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If DName = Empty Then
MsgBox "登陆用户名称不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If DaIp = Empty Then
MsgBox "要连接的数据库的IP地址不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If PsWord = Empty Then
MsgBox "数据库登陆密码不能为空!请输入.......", vbQuestion, "提示"
Connection = False
Exit Function
End If
If App.PrevInstance = True Then
MsgBox "程序已经起动", vbExclamation, "提示"
Connection = False
Exit Function
End If
SQLstring = "Provider=SQLOLEDB.1;Password=" & PsWord & " ;" & _
"Persist Security Info=False;" & _
"User ID=" & DName & ";" & _
"Initial Catalog=" & Catalog & ";" & _
"Data Source=" & DaIp
GACN.ConnectionTimeout = 30
GACN.Open SQLstring
Connection = True
Exit Function
NOERROR:
Select Case Err.Number
Case -2147467259
MsgBox "您输入的服务器地址有误,请重新输入一个正确的地址", vbExclamation, "地址错误"
Load FrmConnection: FrmConnection.Show
Connection = False
Exit Function
Case -2147217843
MsgBox "服务器登陆用户名填写错误,请输入一个正确的用户名", vbExclamation, "用户名错误"
Load FrmConnection: FrmConnection.Show
Connection = False
Exit Function
Case Else
MsgBox "连接:错误代码:" & Err.Number & " 错误描述:" & Err.Description
Load FrmConnection: FrmConnection.Show
Connection = False
End Select
End Function
Public Property Let DataCatalog(ByVal vData As String)
'是要连接的数据库名称
mvarDataCatalog = vData
End Property
Public Property Get DataCatalog() As String
' 是要连接的数据库名称
DataCatalog = mvarDataCatalog
End Property
Public Property Let DataIP(ByVal vData As String)
'是要连接的数据库的IP地址。
mvarDataIP = vData
End Property
Public Property Get DataIP() As String
'是要连接的数据库的IP地址。
DataIP = mvarDataIP
End Property
Public Property Let DataPass(ByVal vData As String)
'是数据库登陆密码
mvarDataPass = vData
End Property
Public Property Get DataPass() As String
'是数据库登陆密码
DataPass = mvarDataPass
End Property
Public Property Let DataName(ByVal vData As String)
'是登陆用户名称。
mvarDataName = vData
End Property
Public Property Get DataName() As String
'是登陆用户名称。
DataName = mvarDataName
End Property