本文介绍了使用backgroundworker检查数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在解决这个问题,我使用backgroundWorker检查数据库是否已打开,这是我的代码:



I already banging my head with this problem, I use backgroundWorker to check if database is OPEN and here's my code for that:

Public Class Form1

    Delegate Sub SetLabelText_Delegate(ByVal [Label] As Label, ByVal [text] As String)

    Dim sqlconnection As New SqlConnection("Data Source=" & My.Settings.Server & ";Initial Catalog=" & My.Settings.Database & ";Integrated Security=false;user id=" & My.Settings.Username & ";password=" & My.Settings.Password & ";Connection Timeout=5;")

    Dim connectionStatus As String

    Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] As String)
        If [Label].InvokeRequired Then
            Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
            Me.Invoke(MyDelegate, New Object() {[Label], [text]})
        Else
            [Label].Text = [text]
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'I store my database information to my.settings then display it on textboxes for manipulation
        TextBox1.Text = My.Settings.Server
        TextBox2.Text = My.Settings.Database
        TextBox3.Text = My.Settings.Username
        TextBox4.Text = My.Settings.Password

        'just getting my computer name
        lblCompName.Text = System.Windows.Forms.SystemInformation.ComputerName

        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        Try

            If sqlconnection.State = ConnectionState.Closed Then
                sqlconnection.Open()
                connectionStatus = "Online"
                'sqlconnection.Open()
                SetLabelText_ThreadSafe(Me.Label1, "Database Status: online")
            End If

        Catch ex As Exception
            connectionStatus = "Offline"
            sqlconnection.Close()
            SetLabelText_ThreadSafe(Me.Label1, "Database Status: offline")
        End Try

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If e.Error IsNot Nothing Then
            Label1.Text = "Database Status: " & connectionStatus
        Else
            Label1.Text = "Database Status: " & connectionStatus
        End If
        BackgroundWorker1.RunWorkerAsync()
    End Sub
end class







在上面的代码中,它实际上是工作时,它显示数据库在线/离线在Visual Studio中重新启动我的程序但是当我运行该程序然后关闭/禁用我的网络连接(我的数据库在其他计算机上)它始终显示数据库在线但我很确定我的电脑和数据库之间已经没有连接(因为我打它)所以它必须显示为数据库是oFFLINE



我是否错过了使用backgroundWOrker的相关信息?哦顺便说一下,我是使用BackgroundWorker的新手。任何帮助或替代解决方案非常感谢谢谢!




In my code above it does actually works, it does display "database is online/offline" when restarting my program within the visual studio but when I run the program then turn-off/disable my network connection(my database is on other computer) it always display "Database is ONLINE" but I'm pretty sure that there is already no connection between my pc and database(because I PING it) so it must be display as "Database is oFFLINE".

Did I missed something regarding of using backgroundWOrker? Oh BTW, I'm new in using BackgroundWorker. Any help or alternative solutions is much appreciated thanks!

推荐答案


If sqlconnection.State = ConnectionState.Closed Then



所以你不要在你的工作人员做任何事情。随后的时间,因为连接没有关闭 - 它可能是打开的,它可能会被打破 - 你不知道: [ ^ ]



而不是这个,在那里放一个finally块,关闭并处理连接,每次重建一个新的。

我也会添加一个Thread.Wait几秒钟 - 或者你的数据库管理员会好起来并且真的很烦你!


So you don't do anything in your worker the second and subsequent times, because the connection is not closed - it could be open, it could be broken - you don't know: http://msdn.microsoft.com/en-us/library/system.data.connectionstate(v=vs.110).aspx[^]

Instead of this, put a finally block in there, which closes and disposes the connection, and rebuild a new one each time.
And I'd add a Thread.Wait for a couple of seconds too - or your DB admin is going to get well and truly annoyed with you!



这篇关于使用backgroundworker检查数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:59
查看更多