[英]MySQL + VB.NET - Populate multiple Datagridviews from multiple tables
I have a Form with a TabControl with 8 tabs, each tab have a Datagridview that needs to be populated each one from a different table of my database. 我有一个带有8个选项卡的TabControl的窗体,每个选项卡都有一个Datagridview,需要从我数据库的不同表中填充每个。 I'd like to know if i need to make all the 8 connections and queries or i can apply the DRY principle and how i can do it. 我想知道我是否需要进行所有8个连接和查询,或者我可以应用DRY原理以及如何做到这一点。
Actually I'm trying populate the Datagridviews but I don't know why I'm just getting populated 3 of them and the rest just stay empty. 实际上,我正在尝试填充Datagridviews,但是我不知道为什么只填充其中的3个,其余的都保持空白。
I would really appreciate your help. 我将衷心感谢您的帮助。
Thanks. 谢谢。
Here is my code: 这是我的代码:
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
I think you could be going abit overboard with the DRY principle. 我认为您可能会对DRY原则有些不满。 When you consider connection pooling , one connection object is all you will need. 考虑连接池时 ,仅需要一个连接对象。 If the each table resides on a different DB, all you need it 8 different connection strings and the pooling will take care of the each of the connections. 如果每个表都位于不同的DB上,则您只需要8个不同的连接字符串,并且池将处理每个连接。 And unless you have a framework in place that could dynamically create you SQL queries for you, you will definately need an SQL select string per Datagrid. 并且,除非您有一个可以为您动态创建SQL查询的框架,否则,每个Datagrid肯定需要一个SQL选择字符串。 Hope you aren't using the VS wizards to do this. 希望您不要使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.