Ejercicios completo Biblioteca Formulario MDI Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.WindowState = FormWindowState.Maximized IsMdiContainer = True End Sub Private Sub EditorialToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditorialToolStripMenuItem.Click Dim frm1 As New frmEditorial frm1.MdiParent = Me frm1.Show() End Sub Private Sub SucursalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SucursalToolStripMenuItem.Click Dim rpt1 As New frmSucursal1 rpt1.MdiParent = Me rpt1.Show() End Sub Private Sub PrestamosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrestamosToolStripMenuItem.Click Dim rpt2 As New frmPrestamosLibro rpt2.MdiParent = Me rpt2.Show() End Sub Private Sub LibrosPrestadosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LibrosPrestadosToolStripMenuItem.Click Dim rpt3 As New frmLibrosPrestados rpt3.MdiParent = Me rpt3.Show() End Sub Private Sub PrestamosPoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrestamosPoToolStripMenuItem.Click Dim rpt4 As New frmPrestamosFecha rpt4.MdiParent = Me rpt4.Show() End Sub End Class Clase conectar Public Class Conectar Public Function concetar_sql() As String Dim sql_con As String sql_con = "Data Source=GENESIS-PC\SQLEXPRESS;Initial Catalog=biblioteca;Integrated Security=True" Return sql_con End Function End Class Clase Editorial Imports System.Data.SqlClient Public Class clsEditorial Private _cod_editorial As Integer Private _nombre_editorial As String Private _pais_editorial As String Private _telefono_editorial As String Public Property cod_editorial() As Integer Get Return Me._cod_editorial End Get Set(ByVal value As Integer) Me._cod_editorial = value End Set End Property Public Property nombre_editorial() As String Get Return Me._nombre_editorial End Get Set(ByVal value As String) Me._nombre_editorial = value End Set End Property Public Property pais_editorial() As String Get Return Me._pais_editorial End Get Set(ByVal value As String) Me._pais_editorial = value End Set End Property Public Property telefono_editorial() As String Get Return Me._telefono_editorial End Get Set(ByVal value As String) Me._telefono_editorial = value End Set End Property Public Sub cargar(ByVal codigo As Integer, ByVal nombre As String, ByVal pais As String, ByVal telefono As String) Me._cod_editorial = codigo Me._nombre_editorial = nombre Me._pais_editorial = pais Me._telefono_editorial = telefono End Sub Public Sub insertar() Dim cadena_conexion As New Conectar() Dim conectar As New SqlConnection Dim cadena_sql As String Try conectar.ConnectionString = cadena_conexion.concetar_sql() cadena_sql = "insert into editorial values(" & Me.cod_editorial & " , '" & Me.nombre_editorial & "', '" & Me.pais_editorial & "', '" & Me.telefono_editorial & "');" Dim vcomando As New SqlCommand(cadena_sql, conectar) conectar.Open() vcomando.ExecuteNonQuery() conectar.Close() MessageBox.Show("Datos insertados con exito", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Public Sub actualizar(ByVal codigo As Integer) Dim cadena_conexion As New Conectar() Dim conectar As New SqlConnection Dim cadena_sql As String Try conectar.ConnectionString = cadena_conexion.concetar_sql() cadena_sql = "update editorial set nombre_editorial = '" & Me.nombre_editorial & "', pais_editorial = '" & Me.pais_editorial & "', telefono_editorial = '" & Me.telefono_editorial & "' where cod_editorial = " & codigo & ";" Dim vcomando As New SqlCommand(cadena_sql, conectar) conectar.Open() vcomando.ExecuteNonQuery() conectar.Close() MessageBox.Show("Datos actualizados con exito", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Public Sub eliminar(ByVal codigo As Integer) Dim cadena_conexion As New Conectar() Dim conectar As New SqlConnection Dim cadena_sql As String Try conectar.ConnectionString = cadena_conexion.concetar_sql() cadena_sql = "delete editorial where cod_editorial = " & codigo & ";" Dim vcomando As New SqlCommand(cadena_sql, conectar) conectar.Open() vcomando.ExecuteNonQuery() conectar.Close() MessageBox.Show("Datos eliminados con exito", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Public Function mostrar_editorial() As DataSet Dim Dim Dim Dim Try cadena_conexion As New Conectar() conectar As New SqlConnection cadena_sql As String ds_editorial As New Data.DataSet 'Creamos el escenario desconectado conectar.ConnectionString = cadena_conexion.concetar_sql cadena_sql = "select cod_editorial, nombre_editorial, pais_editorial, telefono_editorial from editorial;" Dim vAdapter As New SqlDataAdapter(cadena_sql, conectar) conectar.Open() vAdapter.Fill(ds_editorial, "editorial") conectar.Close() Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try Return ds_editorial End Function Public Function buscar_editorial(ByVal nombre As String) As DataSet Dim cadena_conexion As New Conectar() Dim conectar As New SqlConnection Dim cadena_sql As String Dim sel_ds_editorial As New Data.DataSet Try 'Creamos el escenario desconectado conectar.ConnectionString = cadena_conexion.concetar_sql cadena_sql = "select * from editorial where nombre_editorial like('%" & nombre & "%')" Dim vAdapter As New SqlDataAdapter(cadena_sql, conectar) conectar.Open() vAdapter.Fill(sel_ds_editorial, "sel_editorial") conectar.Close() Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try Return sel_ds_editorial End Function End Class DataSet En el BibliotecaDataSet creamos en los TableAdapter las consultas siguientes: --Cree un reporte que contenga la sucursal con mayor número de libros select top 1 sucursal_biblioteca.nombre_sucursal, sucursal_biblioteca.direccion, SUM(copias_libros.num_copias) as "Total" from sucursal_biblioteca, copias_libros where sucursal_biblioteca.id_sucursal = copias_libros.id_sucursal group by sucursal_biblioteca.nombre_sucursal, sucursal_biblioteca.direccion order by Total desc --Cree un reporte con los 10 libros más prestados select top 10 libro.id_libro, libro.titulo, SUM(prestamo.id_libro) as "Total_Prestamos" from libro, prestamo where libro.id_libro = prestamo.id_libro group by libro.id_libro, libro.titulo order by Total_Prestamos desc --Cree un reporte que muestre los libros prestados y el respectivo total de prestamos por día, agrupados por fecha select prestamo.fecha_salida, COUNT(libro.titulo) as "Prestamos_Diarios" from libro, prestamo group by prestamo.fecha_salida --Cree un formulario que permita filtrar bajo un rango de fechas los registros de la tabla préstamos y muestre los resultados en un informe create procedure rpt_prestamos_fecha @fecha_inicio as datetime, @fecha_fin as datetime as select libro.id_libro, libro.titulo, prestamo.fecha_salida from libro, prestamo where prestamo.fecha_salida between @fecha_inicio and @fecha_fin go execute rpt_prestamos_fecha '01/03/2009', '30/03/2009' Reportes Creamos los 4 archivos de Crystal Report de los 4 métodos ADO de los TableAdapter creados en el DataSet (Uno de ellos es un procedimiento almacenado que filtra datos de las fechas) Formularios de reportes El código de los formularios que contienen los reportes es: Cada formulario contiene un CrystalReportViewer Reporte de sucursal que contiene el mayor número de libros Public Class frmSucursal1 Private Sub frmSucursal1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim d_set As New bibliotecaDataSet Dim rpt As New rpt_sucursal Dim d_adapter As New bibliotecaDataSetTableAdapters.sucursal_biblioteca1TableAdapter Me.WindowState = FormWindowState.Maximized d_adapter.Fill(d_set.sucursal_biblioteca1) rpt.SetDataSource(d_set) Me.CrystalReportViewer1.ReportSource = rpt End Sub End Class Reporte de los 10 libros más prestados Public Class frmLibrosPrestados Private Sub frmLibrosPrestados_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.WindowState = FormWindowState.Maximized Dim d_set As New bibliotecaDataSet Dim rpt As New rpt_LibrosPrestados Dim d_adapter As New bibliotecaDataSetTableAdapters.LibrosPrestadosTableAdapter d_adapter.Fill(d_set.LibrosPrestados) rpt.SetDataSource(d_set) Me.CrystalReportViewer1.ReportSource = rpt End Sub End Class Reporte de préstamos agrupados por fechas Public Class frmPrestamosLibro Private Sub frmPrestamosLibro_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.WindowState = FormWindowState.Maximized Dim d_set As New bibliotecaDataSet Dim rpt As New rpt_prestamosLibro Dim d_adapter As New bibliotecaDataSetTableAdapters.PrestamoslibroTableAdapter d_adapter.Fill(d_set.Prestamoslibro) rpt.SetDataSource(d_set) Me.CrystalReportViewer1.ReportSource = rpt End Sub End Class Reporte con filtros por fechas (Procedimiento almacenado) Imports CrystalDecisions.Shared Imports CrystalDecisions.CrystalReports.Engine Public Class frmPrestamosFecha Private Sub btnGenerar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerar.Click Dim d_set As New bibliotecaDataSet Dim rpt As New rpt_PrestamosFecha Dim d_adapter As New bibliotecaDataSetTableAdapters.rpt_prestamos_fechaTableAdapter Dim fecha_ini, fecha_fin As Date Try fecha_ini = CType(Me.txtFechaIni.Text, Date) fecha_fin = CType(Me.txtFechaFin.Text, Date) d_adapter.Fill(d_set.rpt_prestamos_fecha, fecha_ini, fecha_fin) rpt.SetDataSource(d_set) Me.CrystalReportViewer1.ReportSource = rpt Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub End Class