Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SQL_連線狀態模組3.vb 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Module SQL_連線狀態模組3
  2. #Region "——连接测试:避免等待时间过长——"
  3. Dim bConnect As Boolean
  4. Dim BgWorker3 As New ComponentModel.BackgroundWorker
  5. Dim ServerIP As String
  6. ''' <summary>
  7. ''' 测试连接:True-连接成功,False-连接失败
  8. ''' 参数[必选]:服务器IP
  9. ''' 参数[可选]:等待超时时间(秒),默认为1.2秒
  10. ''' </summary>
  11. ''' <returns></returns>
  12. ''' <remarks></remarks>
  13. Function ConnectTest3(ByVal varServerIP As String, Optional ByVal varTimeout As Decimal = 1) As Boolean
  14. AddHandler BgWorker3.DoWork, AddressOf BgWorker3_DoWork
  15. Dim g As Integer
  16. Dim k As Integer
  17. If BgWorker3.CancellationPending Then
  18. BgWorker3.CancelAsync()
  19. '取消BackgroundWorker執行中的工作
  20. End If
  21. Try
  22. If varServerIP = "" Then
  23. Exit Function
  24. End If
  25. ServerIP = varServerIP
  26. If varTimeout <= 0 Then
  27. varTimeout = 1.5
  28. End If
  29. k = Int(varTimeout * 10)
  30. bConnect = False
  31. BgWorker3.WorkerSupportsCancellation = True
  32. BgWorker3.RunWorkerAsync()
  33. For g = 1 To k '1.2s
  34. Threading.Thread.Sleep(100)
  35. If bConnect = True Then
  36. ConnectTest3 = True
  37. Exit Function
  38. End If
  39. Next
  40. ConnectTest3 = False
  41. BgWorker3.CancelAsync()
  42. Catch ex As Exception
  43. MsgBox(ex.Message)
  44. End Try
  45. End Function
  46. '--异步动作
  47. Private Sub BgWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
  48. Call ConnectMSSQLServer()
  49. End Sub
  50. '--连接数据库
  51. Private Function ConnectMSSQLServer() As Boolean
  52. Dim sqlConTest As SqlClient.SqlConnection ' 连接
  53. sqlConTest = New SqlClient.SqlConnection With {
  54. .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"
  55. }
  56. Try
  57. sqlConTest.Open()
  58. sqlConTest.Close()
  59. Catch ex As Exception
  60. Finally
  61. bConnect = True
  62. End Try
  63. End Function
  64. #End Region
  65. End Module