MySQL + VB.NET - TabControl和多个DataGridviews

时间:2011-10-25 01:00:33

标签: mysql vb.net

我的DataGridViews有问题,实际上我有一个带有8个标签的TabControl的表单,每个标签都有一个DataGridView控件。我在表单加载时填充它们,问题是我在这里有一个限制,只有3个DataGridViews被填充,而不是3个特定的,但任何3我首先放在我的代码上。我手动填充它们,执行mysql查询,填充DataTable,然后使数据源等于DataTable。

有什么想法吗?

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

    Public 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

    Public 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

    Public 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

    Public 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
    End Sub


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

1 个答案:

答案 0 :(得分:0)

您可以重构很多代码重复。但是,我认为根本原因是您没有在每个myCommand方法中重新初始化myAdapterFillX()个变量。因此,我首先要为每个conn方法制作myCommandmyAdapterSQLFillX()个变量。

然后我会重构代码以应用DRY(不要重复自己)原则并消除重复代码。