12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- Module SQL_連線狀態模組3
-
- #Region "——连接测试:避免等待时间过长——"
- Dim bConnect As Boolean
- Dim BgWorker3 As New System.ComponentModel.BackgroundWorker
- Dim ServerIP As String
- Private bClosingForm As Boolean = False
- ''' <summary>
- ''' 测试连接:True-连接成功,False-连接失败
- ''' 参数[必选]:服务器IP
- ''' 参数[可选]:等待超时时间(秒),默认为1.2秒
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- 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
- MGB(ex.Message,1)
- 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 Data.SqlClient.SqlConnection ' 连接
- sqlConTest = New Data.SqlClient.SqlConnection
- sqlConTest.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
|