The DataSet schema that you have just created for the Customer table is a data structure. At runtime, code is required to populate the DataSet structure with data from the database. In this section, you create a helper class that populates the DataSet with data.
To create a helper class to populate the DataSet with data
"Imports"[Visual Basic] or "using"[C#] declaration to the top of the class for the System.Data and System.Data.OleDb namespaces.Imports System.Data Imports System.Data.OleDb
using System.Data; using System.Data.OleDb;
Note If you intend to copy and paste the code, note that the Data Source string is for Visual Studio 2005. To be sure that you have the correct file directory path to the xtreme.mdb database, see Location of Xtreme Sample Database.
Private Const CONNECTION_STRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio 8\Crystal Reports\Samples\En\Database\xtreme.mdb"
private const string CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\Microsoft Visual Studio 8\\Crystal Reports\\Samples\\Database\\xtreme.mdb";
Private Const QUERY_STRING As String = "SELECT * FROM CUSTOMER"
private const string QUERY_STRING = "SELECT * FROM CUSTOMER";
Note This simple query string selects all columns, and no criteria. For the Xtreme sample database, this will return only a small amount of data. However, in most cases it is recommended to limit your query by including a WHERE clause and selecting a limited number of columns.
Private Const DATATABLE_NAME As String = "Customer"
private const string DATATABLE_NAME = "Customer";
The following step instructs you to create a DIRECTORY_FILE_PATH constant. This constant is only required if you are creating a Web Site in Visual Studio 2005. Otherwise, skip this step.
Note The code below demonstrates a path for a Web Site.
Private Const DIRECTORY_FILE_PATH As String = "C:\WebSites\VB_Web_Data_DataSets\"
private const string DIRECTORY_FILE_PATH = @"C:\WebSites\CS_Web_Data_DataSets\";
The next step procedure explains how to return a populated DataSet from the CustomerDataSet read-only property.
To create a property that populates the DataSet
"Shared"[Visual Basic] or "static"[C#] modifier so that the class and property can be called directly without needing to be instantiated.Public Shared ReadOnly Property CustomerDataSet() As DataSet
Get
End Get
End Property
public static DataSet CustomerDataSet
{
get
{
}
}
Dim myDataSet As CustomerDataSetSchema = New CustomerDataSetSchema()
CustomerDataSetSchema dataSet = new CustomerDataSetSchema();
ReadXmlSchema() method of the DataSet instance.Dim myDataSet As DataSet = New DataSet() myDataSet.ReadXmlSchema(DIRECTORY_FILE_PATH & "CustomerDataSetSchema.xsd")
DataSet dataSet = new DataSet(); dataSet.ReadXmlSchema(DIRECTORY_FILE_PATH + "CustomerDataSetSchema.xsd");
Dim myOleDbConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
OleDbConnection oleDbConnection = new OleDbConnection(CONNECTION_STRING);
Dim myOleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter(QUERY_STRING, myOleDbConnection)
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(QUERY_STRING, oleDbConnection);
Fill() method of the OleDbDataAdapter instance and pass to it the CustomerDataSetSchema instance and the DATATABLE_NAME constant.myOleDbDataAdapter.Fill(myDataSet, DATATABLE_NAME)
oleDbDataAdapter.Fill(dataSet, DATATABLE_NAME);
Note The Fill() method populates the specified DataTable, within the DataSet instance, with the data retrieved from the database.
Return myDataSet
return dataSet;
The CustomerDataSet property is created and can be called from anywhere in the project.
Continue to Creating a Report that Connects to the DataSet Schema.
Tutorial: Connecting to ADO.NET DataSets | Tutorials and Sample Code | Tutorials' Sample Code Directory