如果mysql查询为空,程序崩溃

时间:2014-04-06 03:46:09

标签: mysql vb.net

我有这个表单显示未完成的传输操作,以便管理员可以完成它们,它会显示正在进行的操作,如果没有正在进行的操作程序崩溃,我试图通过放置一个MsgBox来避免这种情况它不起作用。如何使这个msgbox解决方案有效?

Private Sub editarops_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    conexao.ConnectionString = "server = localhost; user =root; database = frota;"
    conexao.Open()
    comando.Connection = conexao
    strsql = "SELECT codtrab, carga, origem, destino, estado, dataini FROM trabalhos where estado = 'En Route'"
    comando.Connection = conexao
    strsql = "SELECT codtrab, carga, origem, destino, estado, dataini FROM trabalhos where estado = 'En Route'"
    comando.CommandText = strsql
    r3 = comando.ExecuteReader
    If bs.Count = 0 Then

        bs.DataSource = r3


        ComboBox1.Text = bs.Current(0)
        tboxfunc.Text = bs.Current(1)
        TextBox1.Text = bs.Current(2)
        TextBox2.Text = bs.Current(3)
        TextBox3.Text = bs.Current(5)
        Atualizar.Enabled = True
        Cancelar.Enabled = True
        r3.Close()

        Dim sStmt1 As String = "Select codtrab from trabalhos where estado = 'En Route' "
        Dim cmd1 As New MySqlCommand(sStmt1, conexao)
        Dim da1 As MySqlDataAdapter = New MySqlDataAdapter(cmd1)
        Dim dt1 As New DataTable("trabalhos")
        da1.Fill(dt1)
        If dt1.Rows.Count > 0 Then
            ComboBox1.DataSource = dt1
            ComboBox1.DisplayMember = "codtrab"
        Else
            MsgBox("Não existem Funcionários", vbExclamation)
            Me.Close()
            Inicio.Show()
        End If
        da1.Dispose()
        repor()
        TextBox1.Enabled = False
        TextBox2.Enabled = False
        TextBox3.Enabled = False
        TextBox4.Enabled = False
        TextBox5.Enabled = False
        TextBox6.Enabled = False
        tboxfunc.Enabled = False
        btcancelar.Visible = False

    Else
        MsgBox("Não existem Operações En Route", vbExclamation)
        Me.Close()
        Inicio.Show()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

  1. 您应该将连接字符串放在Using块中,或手动处理它。但这通常不会导致崩溃。
  2. 如果Inicio是"呼叫"表单,您可以在不显示Inicio的情况下关闭此表单。
  3. 尝试添加"退出子"在Me.Close之后。