Sei sulla pagina 1di 9

CRYSTAL REPORTS CON FICHERO DE ESQUEMA

El primer paso es crear el fichero de esquema:


Private Sub btnPathXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPathXml.Click Dim lsCadConexion As String= Data Source=.\SQLEXPRESS;Initial Catalog=NorthWind;Integrated Security=True Dim lsQuery As String=SELECT Products.ProductName, [Order Details].ProductID, [Order Details].UnitPrice, [Order Details].Quantity, Orders.OrderID, Orders.OrderDate, Customers.CustomerID, Customers.CompanyName, Customers.City, Customers.Country, Employees.EmployeeID, Employees.LastName FROM Employees INNER JOIN Orders ON Employees.EmployeeID=Orders.EmployeeID INNER JOIN Customers ON Customers.CustomerID=Orders.CustomerID INNER JOIN [Order Details] ON Orders.OrderID=[Order Details].OrderID INNER JOIN Products ON [Order Details].ProductID=Products.ProductID ORDER BY Orders.OrderID Dim loOrigenDatos As New CrearXml(C:\Viculada.xml, lsQuery, lsCadConexion) loOrigenDatos.gP_CrearFicheroXML(C:\Viculada.xml) End Sub Para esto se hace uso de la clase CrearXml cuyo codigo es el siguiente.

Option Explicit On Option Strict On Imports System.Data.SqlClient Public Class CrearXml Private msTabla As String Private msQuery As String Private msCadConexion As String Public Sub New(ByVal vsTabla As String, ByVal vsQuery As String, ByVal vsCadConexion As String) msTabla = vsTabla msQuery = vsQuery msCadConexion = vsCadConexion End Sub Public Function gF_CrearDataTableSql() As DataTable Dim loDataSet As New DataSet Dim loDataTable As DataTable = Nothing Try Using loConexion As New SqlConnection(msCadConexion) Dim loDataAdapter As New SqlDataAdapter(msQuery, loConexion) loDataAdapter.Fill(loDataSet, msTabla) loDataTable = loDataSet.Tables(msTabla) End Using Catch Exp As Exception MessageBox.Show(Exp.Message, "gF_CrearDataTableSql", MessageBoxButtons.OK, MessageBoxIcon.Information) End Try Return loDataTable End Function Public Sub gP_CrearFicheroXML(ByVal FilePath As String) Dim loDataTable As DataTable= gF_CrearDataTableSql() loDataTable.WriteXmlSchema(FilePath) End Sub End Class

El Segundo paso es aadir un elemento nuevo al proyecto.

Se hace click en el + y aparece la siguiente imagen.

Se selecciona el fichero de esquema creado anteriormente y se pulsa Finalizar.

Se pulsa siguiente.

Se pulsa siguiente.

Se pulsa Finalizar, y ya tenemos el fichero Vincula.rpt creado. Manipulando un poco el fichero rpt, lo he dejado asi:

Si se cambia de origen de datos por ejemplo un MDB no importa, el nico requisito es que en el datatable que se le cargue al fichero rpt, contenga todos los campos que contiene el informe.

Para cargar el informe:


Dim loForm As New FrmXXX loForm.Consulta = Me.txtConsulta.Text loForm.FicheroRPT = Me.lblFicheroRpt.Text loForm.CadConexion = "Server=" & Me.txtServidor.Text & ";Initial Catalog=NorthWind;Integrated Security=True" loForm.ShowDialog()

En un Form que contiene 1 CrystalReportViewer (crvInforme), y 1 ReportDocument (rdInforme).


Option Explicit On Option Strict On Imports System.Data.SqlClient Imports CrystalDecisions.CrystalReports.Engine Public Class FrmXXX Private msConsulta As String Private msFicheroRPT As String Private msCadConexion As String Public Property Consulta() As String Get Return msConsulta End Get Set(ByVal value As String) msConsulta = value End Set End Property Public Property FicheroRPT() As String Get Return msFicheroRPT End Get Set(ByVal value As String) msFicheroRPT = value End Set End Property Public Property CadConexion() As String Get Return msCadConexion End Get Set(ByVal value As String) msCadConexion = value End Set End Property Private Sub FrmXXX_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try Me.rdInforme.Load(msFicheroRPT) Catch Exp As Exception MessageBox.Show(Exp.Message, "FrmXXX_Load", MessageBoxButtons.OK, MessageBoxIcon.Information) Me.Close() End Try Dim ldtDataTable As DataTable = lF_dtCrearDataTable()

If ldtDataTable Is Nothing Then MessageBox.Show("No hay datos", "FrmXXX_Load", MessageBoxButtons.OK, MessageBoxIcon.Information) Me.Close() End If Me.rdInforme.SetDataSource(ldtDataTable) Me.crvInforme.ReportSource = Me.rdInforme End Sub Private Function lF_dtCrearDataTable() As DataTable Dim loDataTable As New DataTable Try Using loConexion As New SqlConnection(msCadConexion) Dim loDataAdapter As SqlDataAdapter loDataAdapter = New SqlDataAdapter(msConsulta, loConexion) loDataAdapter.Fill(loDataTable) End Using Catch Exp As SqlException MessageBox.Show(Exp.Message, "lF_dtCrearDataTable", MessageBoxButtons.OK, MessageBoxIcon.Information) End Try Return loDataTable End Function End Class

Potrebbero piacerti anche