Chart for .NET / User's Guide / Data Manipulation / Importing / Importing from OleDbDataReader

Importing from OleDbDataReader

The data contained in the data series can be imported from an OleDbDataReader. This functionality is exposed to the user via the FillFromDataReader methods of the NDataSeries and NDataSeriesCollection classes.

 Importing a single OleDbDataReader column into a data series

The user can import the data contained in an OleDbDataReader column with the help of the FillFromDataReader method of the NDataSeries class. The method receives two arguments - the reader and the column, which must be imported. The data contained in the specified column must be compatible with the data series type (see the   topic for a more detailed description of data series compatibility).

The following example demonstrates a single data series import from an OleDbDataReader. The example assumes that the DataBinging.mdb file contains a table called Sales. The Sales table is supposed to have a column called SalesAmount which is of type Number-Double.

C#
Copy Code
// create a database connection object using the connection string
OleDbConnection myConnection = new
OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source=DataBinding.mdb");

// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand("select * from Sales", myConnection);

// open the connection
myCommand.Connection.Open();

// create the reader
OleDbDataReader myReader;
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

// create a bar chart
NBarSeries bar = (NBarSeries)chart.Series.Add(SeriesType.Bar);

// import the SalesAmount column into the Values data series
bar.Values.FillFromDataReader(myReader, "SalesAmount");
Visual Basic
Copy Code
' create a database connection object using the connection string
OleDbConnection myConnection = New
OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=DataBinding.mdb")

' create a database command on the connection using query
Dim myCommand As OleDbCommand =  New OleDbCommand("select * from Sales",myConnection)

' open the connection
myCommand.Connection.Open()

' create the reader
Dim myReader As OleDbDataReader
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' create a bar chart
Dim bar As NBarSeries = chart.Series.Add(SeriesType.Bar)

' import the SalesAmount column into the Values data series
bar.Values.FillFromDataReader(myReader, "SalesAmount")
 Importing multiple OleDbDataReader columns into a data series collection

You can simultaneously import the data contained in a set of OleDbDataReader columns into several data series contained in a NDataSeriesCollection . This is achieved with the help of the FillFromDataReader method of the NDataSeriesCollection class. The method receives two arguments - the reader and the columns, which must be imported. The first specified column is imported into the first data series, the second column into the second series ... etc.

The following example demonstrates a multiple data series import from an OleDbDataReader. The example code simultaneously imports the SalesAmount and ProductName (of type Text) columns into the Values and Labels data series of a bar chart.

C#
Copy Code
// create a NDataSeriesCollection object
NDataSeriesCollection arrSeries = bar.GetDataSeries(DataSeriesMask.Values |
DataSeriesMask.Labels, DataSeriesMask.None);

// create a string array containing the columns which must be imported
string[] arrCollumns = { "SalesAmount", "ProductName" };

// import the columns into the data series
arrSeries.FillFromDataReader(myReader, arrCollumns);
Visual Baisc
Copy Code
' create a NDataSeriesCollection object
Dim arrSeries As NDataSeriesCollection =  bar.GetDataSeries(DataSeriesMask.Values Or DataSeriesMask.Labels,DataSeriesMask.None)

' create a string array containing the columns which must be imported
Dim arrCollumns() As String = {"SalesAmount", "ProductName"}

' import the columns into the data series
arrSeries.FillFromDataReader(myReader, arrCollumns)
 Related Examples
Windows forms: All Examples\Data Manipulation\Importing\From OleDbDataReader
See Also