Public Function IsServerOK() As Boolean On Error GoTo err Dim conn As ADODB.Connection Set conn = New ADODB.Connection conn.ConnectionTimeout = 2 '设置连接超时,2s内无法连接视为服务器不可用 conn.Open "Provider=SQLOLEDB.1;Persist Security Info=True;UID=sa;PWD=sa;Initial Catalog=TEST;Data Source=192.168.1.1" '连接服务器 conn.Close Set conn = Nothing IsServerOK= True Exit Function err: Set conn = Nothing IsServerOK= False End Function