MySQL + VB.NET - 从多个表中填充多个Datagridviews

时间:2011-10-26 15:43:48

标签: mysql vb.net

我有一个带有8个选项卡的TabControl的表单,每个选项卡都有一个Datagridview,需要从我的数据库的不同表中填充每个。我想知道我是否需要完成所有8个连接和查询,或者我可以应用DRY原则以及我如何做到这一点。

实际上我正在尝试填充Datagridviews,但我不知道为什么我只是填充其中的3个而其余的只是保持空白。

我真的很感谢你的帮助。

感谢。

这是我的代码:

Imports MySql.Data.MySqlClient
Imports System.Data

Public Class frmMantenimiento
    Dim conn As MySqlConnection
    Dim myCommand As New MySqlCommand
    Dim myAdapter As New MySqlDataAdapter
    Dim dataEmp As New DataTable
    Dim dataNomi As New DataTable
    Dim dataPres As New DataTable
    Dim dataGast As New DataTable
    Dim dataAmon As New DataTable
    Dim dataHora As New DataTable
    Dim SQL As String

    Public Sub FillEmps()
        conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

        myCommand.Connection = conn
        SQL = "SELECT IDEmpleado as ID, Nombre, Cargo FROM Empleados"

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(dataEmp)

                dgvConsEmp.DataSource = dataEmp

                dgvConsEmp.Columns(0).Width = 30
                dgvConsEmp.Columns(1).Width = 200
                dgvConsEmp.Columns(2).Width = 200

            Catch myerror As MySqlException
                MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub

    Public Sub FillNom()

        conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

        myCommand.Connection = conn
        SQL = String.Empty
        SQL = "SELECT IDNomina as ID, Fecha, MontoBruto as 'Monto Bruto', MontoNeto as 'Monto Neto', CantidadEmpleados as Empleados FROM Nomina"

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(dataNomi)

                dgvConsNomi.DataSource = dataNomi

                dgvConsNomi.Columns(0).Width = 30

            Catch myerror As MySqlException
                MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub

    Private Sub FillPres()

        conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

        myCommand.Connection = conn
        SQL = String.Empty
        SQL = "SELECT IDPrestamo as ID, IDEmpleado as 'ID Empleado', MontoInicial as Monto, Fecha, MontoDescuento as Descuento, FormaDescuento as Recurrencia, Status FROM Prestamos"

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(dataPres)

                dgvConsPres.DataSource = dataPres

                dgvConsPres.Columns(0).Width = 30

            Catch myerror As MySqlException
                MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub

    Private Sub FillGast()

        conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

        myCommand.Connection = conn
        SQL = String.Empty
        SQL = "SELECT IDGasto as ID, IDEmpleado as 'ID Empleado', Monto, Fecha, Concepto, IDUsuario as Creador FROM GastosEmpleados"

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(dataGast)

                Me.dgvConsGast.DataSource = dataGast

                Me.dgvConsGast.Columns(0).Width = 30

            Catch myerror As MySqlException
                MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub

    Private Sub FillAmon()

        conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

        myCommand.Connection = conn
        SQL = "SELECT IDAmonestacion as ID, TipoAmonestacion as Tipo, Descripcion, IDUsuario as Creador FROM Amonestaciones"

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(dataAmon)

                dgvConsAmon.DataSource = dataAmon

                dgvConsAmon.Columns(0).Width = 30

            Catch myerror As MySqlException
                MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub

    Private Sub FillHora()

        conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

        myCommand.Connection = conn
        SQL = "SELECT IDControlHorario as ID, IDEmpleado as 'ID Empleado', Fecha, Hora FROM ControlHorario"

        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(dataHora)

                dgvConsHora.DataSource = dataHora

                dgvConsHora.Columns(0).Width = 30

            Catch myerror As MySqlException
                MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
        dgvConsHora.Refresh()
    End Sub


    Private Sub frmMantenimiento_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillEmps()
        FillNom()
        FillPres()
        FillHora()
        FillGast()
        FillAmon()
    End Sub

1 个答案:

答案 0 :(得分:0)

我认为你可能会因为DRY原则而过火了。当您考虑connection pooling时,您将需要一个连接对象。如果每个表都驻留在不同的DB上,那么您只需要8个不同的连接字符串,并且池将处理每个连接。除非你有一个可以为你动态创建SQL查询的框架,否则每个Datagrid肯定需要一个SQL选择字符串。希望你没有使用VS向导来做到这一点。

Public Sub FillEmps() 

    myCommand.Connection = conn 
    SQL = "SELECT IDEmpleado as ID, Nombre, Cargo FROM Empleados" 

    Try 
       myCommand.Connection = conn 
       myCommand.CommandText = SQL 

       myAdapter.SelectCommand = myCommand 
       myAdapter.Fill(dataEmp) 

       dgvConsEmp.DataSource = dataEmp 

       dgvConsEmp.Columns(0).Width = 30 
       dgvConsEmp.Columns(1).Width = 200 
       dgvConsEmp.Columns(2).Width = 200 

    Catch myerror As MySqlException 
        MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message) 
    End Try 
End Sub 


Private Sub frmMantenimiento_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")

    Try
        conn.Open()

        FillEmps() 
        FillNom() 
        FillPres() 
        FillHora() 
        FillGast() 
        FillAmon() 
    Catch
        If conn.State <> ConnectionState.Closed Then conn.Close() 
    Finally
        If conn.State <> ConnectionState.Closed Then conn.Close() 
    End Try

End Sub