Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SQL_連線狀態模組2.vb 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Module SQL_連線狀態模組2
  2. #Region "——连接测试:避免等待时间过长——"
  3. Dim bConnect As Boolean
  4. Dim BgWorker2 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) As Boolean
  14. AddHandler BgWorker2.DoWork, AddressOf BgWorker2_DoWork
  15. Dim g As Integer
  16. Dim k As Integer
  17. If BgWorker2.CancellationPending Then
  18. BgWorker2.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 = 3
  28. End If
  29. k = Int(varTimeout * 10)
  30. bConnect = False
  31. BgWorker2.WorkerSupportsCancellation = True
  32. BgWorker2.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. BgWorker2.CancelAsync()
  42. Catch ex As Exception
  43. MGB(ex.Message,1)
  44. End Try
  45. End Function
  46. '--异步动作
  47. Private Sub BgWorker2_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. Dim catalogName As String = "GF" & 資料庫編號 & "-ERP-SYS"
  55. If Strings.Left(ServerIP, 3) = "106" Then
  56. sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=GF00-ERP-SYS;Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
  57. Else
  58. sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=" & catalogName & ";Persist Security Info=True;User ID=b70340;Password=0911274990;Max pool size = 200;Connection Timeout=0"
  59. End If
  60. Try
  61. sqlConTest.Open()
  62. sqlConTest.Close()
  63. Catch ex As Exception
  64. Finally
  65. bConnect = True
  66. End Try
  67. End Function
  68. #End Region
  69. End Module