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_連線狀態模組2.vb 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Module SQL_連線狀態模組2
  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 ConnectTest2(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. ConnectTest2 = True
  37. Exit Function
  38. End If
  39. Next
  40. ConnectTest2 = False
  41. BgWorker1.CancelAsync()
  42. Catch ex As Exception
  43. MsgBox(ex.Message)
  44. End Try
  45. #Disable Warning BC42353 ' 函式在所有程式碼路徑上皆不會傳回值
  46. End Function
  47. #Enable Warning BC42353 ' 函式在所有程式碼路徑上皆不會傳回值
  48. '--异步动作
  49. Private Sub BgWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
  50. Call ConnectMSSQLServer()
  51. End Sub
  52. '--连接数据库
  53. Private Function ConnectMSSQLServer() As Boolean
  54. Dim sqlConTest As Data.SqlClient.SqlConnection ' 连接
  55. sqlConTest = New Data.SqlClient.SqlConnection
  56. Dim catalogName As String = "GF" & 資料庫編號.ToString("00") & "-ERP-SYS"
  57. sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=" & catalogName & ";Persist Security Info=True;User ID=b70340;Password=0911274990;Max pool size = 200;Connection Timeout=0"
  58. Try
  59. sqlConTest.Open()
  60. sqlConTest.Close()
  61. Catch ex As Exception
  62. Finally
  63. bConnect = True
  64. End Try
  65. #Disable Warning BC42353 ' 函式在所有程式碼路徑上皆不會傳回值
  66. End Function
  67. #Enable Warning BC42353 ' 函式在所有程式碼路徑上皆不會傳回值
  68. #End Region
  69. End Module