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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = 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. Dim catalogName As String = "GF" & 資料庫編號 & "-ERP-SYS"
  55. If Strings.Left(ServerIP, 3) = "106" Then
  56. sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=" & catalogName & ";Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
  57. Else
  58. If 廟 = True Then
  59. sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=" & catalogName & ";Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0"
  60. Else
  61. sqlConTest.ConnectionString = "Data Source=" + ServerIP + ";Initial Catalog=" & catalogName & ";Persist Security Info=True;User ID=b70340;Password=0911274990;Max pool size = 200;Connection Timeout=0"
  62. End If
  63. End If
  64. Try
  65. sqlConTest.Open()
  66. sqlConTest.Close()
  67. Catch ex As Exception
  68. Finally
  69. bConnect = True
  70. End Try
  71. End Function
  72. #End Region
  73. End Module