Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SQL_連線狀態模組4.vb 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Module SQL_連線狀態模組4
  2. #Region "——连接测试:避免等待时间过长——"
  3. Dim bConnect As Boolean
  4. Dim BgWorker4 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 ConnectTest4(ByVal varServerIP As String, Optional ByVal varTimeout As Decimal = 1) As Boolean
  14. AddHandler BgWorker4.DoWork, AddressOf BgWorker4_DoWork
  15. Dim g As Integer
  16. Dim k As Integer
  17. If BgWorker4.CancellationPending Then
  18. BgWorker4.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. BgWorker4.WorkerSupportsCancellation = True
  32. BgWorker4.RunWorkerAsync()
  33. For g = 1 To k '1.2s
  34. Threading.Thread.Sleep(100)
  35. If bConnect = True Then
  36. ConnectTest4 = True
  37. Exit Function
  38. End If
  39. Next
  40. ConnectTest4 = False
  41. BgWorker4.CancelAsync()
  42. Catch ex As Exception
  43. MGB(ex.Message,1)
  44. End Try
  45. End Function
  46. '--异步动作
  47. Private Sub BgWorker4_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