How to push data from DataTables into EViews via COM

For questions regarding the import, export and manipulation of data in EViews, including graphing and basic statistics.

Moderators: EViews Gareth, EViews Steve, EViews Moderator, EViews Jason

EViews Steve
EViews Developer
Posts: 844
Joined: Tue Sep 16, 2008 3:00 pm
Location: Irvine, CA

How to push data from DataTables into EViews via COM

Postby EViews Steve » Thu Apr 29, 2010 10:45 am

Someone recently asked this question and I thought it deserved it's own forum post because of the popularity of Datasets and DataTables out there.

A DataTable is a virtual representation of a table in a database. It can have multiple columns and multiple rows. It is equivalent to an EViews workfile where each column in the table is represented as a series object in the workfile.

Our COM methods Put, PutSeries, and PutGroup, do not currently support reading a passed in DataTable or Dataset object. We only support reading Excel Range objects and 1 or 2 dimensional arrays.

Because of this, to get the data out of a DataTable and into EViews requires that you convert each column into a 1-dimensional array. Once you have the array, you can call PutSeries with that array and the name of the column as the series name.

Here's an example of doing this in VB.NET. Assume the variable 'ds' is the Dataset object that contains one DataTable that you'd like to push to EViews. In this example, I create a new undated workfile that has the same number of rows as found in the DataTable to store the data.

VB.NET Example:

Code: Select all

'launch or connect to EViews Dim mgr As New EViews.Manager Dim app As EViews.Application = mgr.GetApplication(EViews.CreateType.ExistingOrNew) 'for debugging only, show EViews app.Show() 'create an undated workfile with the right number of observations app.Run("create u " & ds.Tables(0).Rows.Count.ToString) For Each col As DataColumn In ds.Tables(0).Columns Dim al As New ArrayList(ds.Tables(0).Rows.Count) For Each row As DataRow In ds.Tables(0).Rows al.Add(row(col)) 'DBNull values are ok here Next Try app.PutSeries(col.ColumnName, al.ToArray()) Catch ex As Exception MsgBox(ex.Message) End Try Next 'clean up app = Nothing mgr = Nothing GC.Collect()
The above example should work with most data types found in database tables. Notable exceptions include binary and timestamp types (dates and time types should be fine).

Now if the DataTable contains only columns of a single data type (all numeric, or all string), then you could push everything into EViews at once by converting the DataTable into a 2-dimensional array, then calling PutGroup with the data, along with the column names.

We'll most likely add support for reading DataTable objects directly in the future. But until then, this workaround should suffice.

Steve

Return to “Data Manipulation”

Who is online

Users browsing this forum: No registered users and 1 guest