增加注释

This commit is contained in:
xiaochou164 2023-08-15 19:32:12 +08:00
parent ad9b9bdd39
commit baafcf0cce
1 changed files with 9 additions and 8 deletions

View File

@ -1,6 +1,6 @@
'----关闭数据库连接,释放资源 '----关闭数据库连接,释放资源
Public Sub closeConn() Public Sub closeConn()
On Error Resume Next On Error Resume Next '错误仍继续执行
If conn Is Nothing Then Exit Sub If conn Is Nothing Then Exit Sub
conn.Close conn.Close
Set conn = Nothing Set conn = Nothing
@ -13,25 +13,26 @@ Public Sub AutoRun()
On Error Resume Next On Error Resume Next
Dim strDBinf As String Dim strDBinf As String
Application.StatusBar = "正在连接数据库……" Application.StatusBar = "正在连接数据库……" '在Excel状态栏上显示提示
If conn Is Nothing Then If conn Is Nothing Then
Set conn = New ADODB.Connection Set conn = New ADODB.Connection '初始化conn为一个新的ADO连接对象
Set RES = CreateObject("ADODB.Recordset") Set RES = CreateObject("ADODB.Recordset")
strDBinf = "DSN=mysqlinklexcel32" '标准语句 strDBinf = "DSN=mysqlinklexcel32" '标准语句,因为在DSN中已经配置了IP、端口、用户名、密码等信息此处可不用再配置
Call conn.Open(strDBinf) Call conn.Open(strDBinf) '尝试使用DSN链接数据库
If VBA.Err.Number <> 0 Then If VBA.Err.Number <> 0 Then '当VBA错误数量不为0时,则判断此时的DSN链接失败,提示使用64位的进行连接
strDBinf = "DSN=mysqlinklexcel64" '标准语句 strDBinf = "DSN=mysqlinklexcel64" '标准语句
VBA.MsgBox "32位DSN连接失败,尝试使用mysqllinkexcel6连接!" VBA.MsgBox "32位DSN连接失败,尝试使用mysqllinkexcel6连接!"
Call conn.Open(strDBinf) Call conn.Open(strDBinf)
End If End If
If VBA.Err.Number > 1 Then If VBA.Err.Number > 1 Then '当VBA错误数量>1时,则判断此时的DSN链接失败
VBA.MsgBox "连接异常,请检查DB环境!" VBA.MsgBox "连接异常,请检查DB环境!"
Set conn = Nothing Set conn = Nothing
End If End If
'当VBA错误数量<1时(也就是没有错误时),则判断此时的DSN链接成功
Application.StatusBar = "数据库连接成功" Application.StatusBar = "数据库连接成功"
End If End If
End Sub End Sub