Geen omschrijving
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_連線狀態模組1.vb 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. Module SQL_連線狀態模組1
  2. #Region "——连接测试:避免等待时间过长——"
  3. Dim bConnect As Boolean
  4. Dim BgWorker1 As New System.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 ConnectTest1(ByVal varServerIP As String, Optional ByVal varTimeout As Decimal = 1.5) As Boolean
  14. AddHandler BgWorker1.DoWork, AddressOf BgWorker1_DoWork
  15. Dim g As Integer
  16. Dim k As Integer
  17. If BgWorker1.CancellationPending Then
  18. BgWorker1.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. BgWorker1.WorkerSupportsCancellation = True
  32. BgWorker1.RunWorkerAsync()
  33. For g = 1 To k '1.2s
  34. Threading.Thread.Sleep(100)
  35. If bConnect = True Then
  36. ConnectTest1 = True
  37. Exit Function
  38. End If
  39. Next
  40. ConnectTest1 = False
  41. BgWorker1.CancelAsync()
  42. Catch ex As Exception
  43. MGB(ex.Message, 1)
  44. End Try
  45. End Function
  46. '--异步动作
  47. Private Sub BgWorker1_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 Data.SqlClient.SqlConnection ' 连接
  53. sqlConTest = New Data.SqlClient.SqlConnection
  54. 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"
  55. Try
  56. sqlConTest.Open()
  57. sqlConTest.Close()
  58. Catch ex As Exception
  59. Finally
  60. bConnect = True
  61. End Try
  62. End Function
  63. #End Region
  64. End Module