资源管理站

excel VBA判断网络和数据库是否连通

zyglz 3739 0

特别声明:本文为原创,可自由转载、引用,但需署名作者且注明文章出处,如有侵权请联系!

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

评论列表 (已有0条评论)

消灭零回复

发表评论 (已有0条评论)

icon_lol.gif2016zhh.gif2016fendou.gif2016lengh.gificon_exclaim.gif2016gg.gif2016yhh.gificon_cry.gif2016bs.gif2016qd.gif2016bz.gificon_eek.gif2016ka.gif2016zhem.gificon_confused.gif2016qq.gif2016db.gif2016jk.gif2016tuu.gif2016zk.gif2016kk.gificon_neutral.gif