Module SQL_連線狀態模組2 #Region "——连接测试:避免等待时间过长——" Dim bConnect As Boolean Dim BgWorker2 As New System.ComponentModel.BackgroundWorker Dim ServerIP As String ''' ''' 测试连接:True-连接成功,False-连接失败 ''' 参数[必选]:服务器IP ''' 参数[可选]:等待超时时间(秒),默认为1.2秒 ''' ''' ''' Function ConnectTest2(ByVal varServerIP As String, Optional ByVal varTimeout As Decimal = 1) As Boolean AddHandler BgWorker2.DoWork, AddressOf BgWorker2_DoWork Dim g As Integer Dim k As Integer If BgWorker2.CancellationPending Then BgWorker2.CancelAsync() '取消BackgroundWorker執行中的工作 End If Try If varServerIP = "" Then Exit Function End If ServerIP = varServerIP If varTimeout <= 0 Then varTimeout = 3 End If k = Int(varTimeout * 10) bConnect = False BgWorker2.WorkerSupportsCancellation = True BgWorker2.RunWorkerAsync() For g = 1 To k '1.2s Threading.Thread.Sleep(100) If bConnect = True Then ConnectTest2 = True Exit Function End If Next ConnectTest2 = False BgWorker2.CancelAsync() Catch ex As Exception MGB(ex.Message,1) End Try End Function '--异步动作 Private Sub BgWorker2_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 Dim catalogName As String = "GF" & 資料庫編號 & "-ERP-SYS" If Strings.Left(ServerIP, 3) = "106" Then sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=GF00-ERP-SYS;Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0" Else sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=" & catalogName & ";Persist Security Info=True;User ID=b70340;Password=0911274990;Max pool size = 200;Connection Timeout=0" End If Try sqlConTest.Open() sqlConTest.Close() Catch ex As Exception Finally bConnect = True End Try End Function #End Region End Module