Module SQL_連線狀態模組3
#Region "——连接测试:避免等待时间过长——"
Dim bConnect As Boolean
Dim BgWorker3 As New ComponentModel.BackgroundWorker
Dim ServerIP As String
'''
''' 测试连接:True-连接成功,False-连接失败
''' 参数[必选]:服务器IP
''' 参数[可选]:等待超时时间(秒),默认为1.2秒
'''
'''
'''
Function ConnectTest3(ByVal varServerIP As String, Optional ByVal varTimeout As Decimal = 1) As Boolean
AddHandler BgWorker3.DoWork, AddressOf BgWorker3_DoWork
Dim g As Integer
Dim k As Integer
If BgWorker3.CancellationPending Then
BgWorker3.CancelAsync()
'取消BackgroundWorker執行中的工作
End If
Try
If varServerIP = "" Then
Exit Function
End If
ServerIP = varServerIP
If varTimeout <= 0 Then
varTimeout = 1.5
End If
k = Int(varTimeout * 10)
bConnect = False
BgWorker3.WorkerSupportsCancellation = True
BgWorker3.RunWorkerAsync()
For g = 1 To k '1.2s
Threading.Thread.Sleep(100)
If bConnect = True Then
ConnectTest3 = True
Exit Function
End If
Next
ConnectTest3 = False
BgWorker3.CancelAsync()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
'--异步动作
Private Sub BgWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Call ConnectMSSQLServer()
End Sub
'--连接数据库
Private Function ConnectMSSQLServer() As Boolean
Dim sqlConTest As SqlClient.SqlConnection ' 连接
sqlConTest = New SqlClient.SqlConnection With {
.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=HX-GPS-ERP-SYS;Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
}
Try
sqlConTest.Open()
sqlConTest.Close()
Catch ex As Exception
Finally
bConnect = True
End Try
End Function
#End Region
End Module