请教关于ADO连接的问题,高手请帮忙看看!急啊!
正在开发的软件需要检测网络(局域网)状态,写了一个检测的子过程(使用ADO),并在程序界面用timer来不断调用检测网络。运行软件时假设网络为通,timer运行时间为65535毫秒(每隔65秒检测一次网络),当网络由通变断时,timer能够检测出来,并显示网络故障的提示,而当网络由断变通时,却检测不到了。一直还是断的状态。不知道怎么回事,请大家帮忙看一下,在线等!先谢了,下面是具体语句:
'TimeSyn模块功能:1.检测网络状态;2.同步服务器时间
Public Sub TimeSyn()
On Error GoTo errorhandle
Dim sqlstr As String
Dim RetCn As ADODB.Connection
Dim RetRs As ADODB.Recordset
Dim SerDate As Date
Set RetCn = New ADODB.Connection
RetCn.ConnectionTimeout = 3
RetCn.Open "Driver={SQL Server};Server= " & ServerName & ";Database=lxsf;Uid=sa;Pwd= "
If Connect = True Then
Set RetRs = New ADODB.Recordset
sqlstr = "select getdate() as Nowdate "
RetRs.Open sqlstr, RetCn, adOpenDynamic, adLockOptimistic
If Not RetRs.EOF Then
RetRs.MoveFirst
SerDate = CDate(Format(RetRs.Fields(0).value, "yyyy-mm-dd hh:mm:ss "))
End If
'MsgBox SerDate
Set RetRs = Nothing
Set RetCn = Nothing
Date = SerDate
Time = SerDate
Connect = True
End If
Exit Sub
errorhandle:
Connect = False
Resume Next
End Sub
timer过程:
Private Sub Timer1_Timer()
'每隔1.1分钟:1检测网络通断,如果网断,则给出提示(不同步与服务器的时间);2.如果网络通,与服务器时间同步
Call TimeSyn
If Connect = False Then
Label1.caption = "网络故障 " '如果网络故障,则屏幕上给出提示
Else
Label1.caption = " " '如果网络通畅,则将提示隐藏
End If
End Sub
[解决办法]
If Connect = True Then
这个判断有问题吧
判断RetCn。state
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
[解决办法]
不知道这个软件是用来做什么的,这种方法来判断网络连接?好坏不说那首先保证SQL服务器在正常工作,SQL服务正常开启。就代码而言,feiyun0112(http://feiyun0112.cnblogs.com/) 说的对。如下试试:
Function TimeSyn() As Boolean
Dim RetCn As ADODB.Connection
Set RetCn = New ADODB.Connection
RetCn.Open "Driver={SQL Server};Server= " & ServerName & ";Database=lxsf;Uid=sa;Pwd= "
If RetCn.State = adStateOpen Then
TimeSyn = True
Else
TimeSyn = True
End If
End Function
Private Sub Timer1_Timer()
If TimeSyn = False Then
Label1.Caption = "网络故障 " '如果网络故障,则屏幕上给出提示
Else
Label1.Caption = " " '如果网络通畅,则将提示隐藏
End If
End Sub
[解决办法]
上面有错误:
Function TimeSyn() As Boolean
On Error GoTo errorhandle
Dim RetCn As ADODB.Connection
Set RetCn = New ADODB.Connection
RetCn.Open "Provider = SQLOLEDB.1;Persist Security Info = False; " & _
"User ID = sa;Password = MOFZaCW3Ob;Data Source = SERVER; " & _
"Initial Catalog = YSLH "
If RetCn.State = adStateOpen Then
TimeSyn = True
Else
TimeSyn = False
End If
Set RetCn = Nothing
Exit Function
errorhandle:
TimeSyn = False
Set RetCn = Nothing
End Function