特别声明:本文为原创,可自由转载、引用,但需署名作者且注明文章出处,如有侵权请联系!
excel VBA判断网络和数据库是否连通
在使用excel VBA工程时,有时候因程序的需求,需要程序判断网络和数据库是否连通。以下代码分享给大家。
一、判断网络是否接通
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" _
Alias "InternetGetConnectedStateExA" _
(ByRef lpdwFlags As Long, _
ByVal lpszConnectionName As String, _
ByVal dwNameLen As Long, _
ByVal dwReserved As Long _
) As Long
Sub CheckInternet()
Dim dwFlags As Long
Dim strBuf As String
Dim lRet As Long
strBuff = String$(513, 0)
lRet = InternetGetConnectedStateEx(dwFlags, strBuf, 512, 0&)
If lRet = 1 Then
MsgBox "已经连接网络"
Else
MsgBox "网络连接失败"
End If
End Sub
二、判断数据库是否能连接上
Sub 连接数据库()
Dim cnn
Set cnn = CreateObject("ADODB.Connection")
On Error GoTo Err
cnn.Open "Provider=SQLOLEDB;Data Source=xmgy-PC;Initial Catalog=GY-MIS;Uid=sa;PWD=123;Persist Security Info=false"
MsgBox "连接数据库成功!", vbInformation + vbOKOnly, "提示"
cnn.Close
Set cnn = Nothing
Exit Sub
Err:
MsgBox "数据库连接失败,请确认网络/数据库连接"
End Sub
文章来源:
zyglz
版权声明:本文为原创,可自由转载、引用,但需署名作者且注明文章出处,如有侵权请联系!
评论列表 (已有0条评论)
消灭零回复