Bez popisu
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.4KB

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