Module SQL_連線狀態模組1
#Region "——连接测试:避免等待时间过长——"
Dim bConnect As Boolean
Dim BgWorker1 As New System.ComponentModel.BackgroundWorker
Dim ServerIP As String
'''
''' 测试连接:True-连接成功,False-连接失败
''' 参数[必选]:服务器IP
''' 参数[可选]:等待超时时间(秒),默认为1.2秒
'''
'''
'''
Function ConnectTest1(ByVal varServerIP As String, Optional ByVal varTimeout As Decimal = 1.5) As Boolean
AddHandler BgWorker1.DoWork, AddressOf BgWorker1_DoWork
Dim g As Integer
Dim k As Integer
If BgWorker1.CancellationPending Then
BgWorker1.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
BgWorker1.WorkerSupportsCancellation = True
BgWorker1.RunWorkerAsync()
For g = 1 To k '1.2s
Threading.Thread.Sleep(100)
If bConnect = True Then
ConnectTest1 = True
Exit Function
End If
Next
ConnectTest1 = False
BgWorker1.CancelAsync()
Catch ex As Exception
MsgBox(ex.Message, 1)
End Try
End Function
'--异步动作
Private Sub BgWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Call ConnectMSSQLServer()
End Sub
'--连接数据库
Private Function ConnectMSSQLServer() As Boolean
Dim sqlConTest As Data.SqlClient.SqlConnection ' 连接
sqlConTest = New Data.SqlClient.SqlConnection
sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=rt2021;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