APISDWTutorialVBNET

From ReliaWiki
Revision as of 17:55, 28 October 2015 by Kate Racaza (talk | contribs)
Jump to navigation Jump to search

Template:InProgress

APIWiki.png


<< Back to Quick Start Guide

Synthesis Data Warehouse (SDW) Tutorial

This VB.NET tutorial will demonstrate one approach to read data from an Excel file and extract it for the SDW. Note that this tutorial is for demonstration purposes only; it does not include any exception handling and its performance may be slow. To complete this tutorial successfully, please use the sample Excel data that was provided for this tutorial.



Transfer Data from an Excel File to the SDW

1. We'll begin with the RawDataSet class, which represents an SDW data collection. We'll then use the ExtractedType property to specify that the data collection is for use with Weibull++.

Create a new form and start with the following code to create an SDW data collection.

VB.NET

 'Declare a new instance of the RawDataSet class. 
  Dim DataCollection As New RawDataSet

 'Name the data collection "New Data Collection," and then specify that 
 'it is for use with Weibull++. 
  DataCollection.ExtractedName = "New Data Collection"
  DataCollection.ExtractedType = RawDataSetType.Weibull

2. Next, we'll use the RawData class to represent a single row of data, and then use the AddDataRow method to add the row to the data collection.

The following code demonstrates one way to extract a row of data from the Excel file. First, the code opens a connection to the Excel file and specifies the active sheet (in this case, sheet #1). Then For i = 2, the code creates a new RawData object and sets its properties by using the values from row #2 of the Excel file. After the code finishes reading the row of data, it adds the object to the data collection. When the code reaches Next i, it increases i with 1 and jumps back to the For statement, which adds another new RawData object, sets its properties with the data from row #3 of the Excel sheet, and adds the row to the data collection. The loop continues until i reaches the specified number of rows (MaxRow). Finally, the code closes the connection to the Excel file.

VB.NET

 'Declare a new instance of the RawData class. 
  Dim Row As New RawData

 'Open the Excel file. The following code assumes that your 
 'sample data is saved in the C drive. 
  Dim Excel As New Application
  Dim Workbook As Workbook
  Dim Sheet As Worksheet

  Workbook = Excel.Workbooks.Open("C:\SampleData.xlsx")
  Sheet = Workbook.Sheets(1)

 'Read each row of data in the Excel file. 
  Dim i As Integer, MaxRow As Integer
  MaxRow = 20

  For i = 2 To MaxRow
      Row = New RawData

     'Set the properties for the current row of data. 
      Row.StateFS = sheet.Cells(i, 1).text
      Row.StateTime = sheet.Cells(i, 2).value
      Row.FailureMode = sheet.Cells(i, 3).text

     'Add the current row to the data collection. 
      Call DataCollection.AddDataRow(Row)
  Next i

 'Close the Excel file. 
  Workbook.Close()
  Excel.Quit()

3. Now that the data from the Excel file have been copied to the data collection, the next step is to save the data collection to the Synthesis repository.

First, connect to the Synthesis repository and then use the SaveRawDataSet method to save the data collection to the repository.

VB.NET

 'Connect to the Synthesis repository. The following code assumes that 
 'a standard repository called "RSRepository1.rsr10" exists in the C: drive.   
  Dim MyRepository As New Repository   
  MyRepository.ConnectToRepository ("C:\RSRepository1.rsr10")

 'Save the extracted data collection to the repository. 
  MyRepository.DataWarehouse.SaveRawDataSet(DataCollection)

Test the Code

Let's verify whether the code can import the data successfully into the SDW.

Below is the complete code list for this example. On the Debug menu, click Start to run the application. Then verify that the data were imported by launching Weibull++, opening the SDW (Home > Synthesis > Synthesis Data Warehouse) and then selecting the new data collection. (You may need to display the StateTime, StateFS and FailureMode columns by right-clicking a column header and choosing Select Columns.)

VB.NET

Imports Synthesis.API 
Imports Microsoft.Office.Interop.Excel

 Public Class Form1
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

      'Declare a new instance of the RawDataSet class. 
       Dim DataCollection As New RawDataSet
 
      'Name the data collection "New Data Collection," and then specify that 
      'it is for use with Weibull++. 
       DataCollection.ExtractedName = "New Data Collection"
       DataCollection.ExtractedType = RawDataSetType.Weibull

      'Declare a new instance of the RawData class. 
       Dim Row As New RawData
 
      'Open the Excel file. The following code assumes that your 
      'sample data is saved in the C drive. 
       Dim Excel As New Application
       Dim Workbook As Workbook
       Dim Sheet As Worksheet

       Workbook = Excel.Workbooks.Open("C:\SampleData.xlsx")
       Sheet = Workbook.Sheets(1)

      'Read each row of data in the Excel file. 
       Dim i As Integer, MaxRow As Integer
       MaxRow = 20
 
       For i = 2 To MaxRow
           Row = New RawData

           'Set the properties for the current row of data. 
            Row.StateFS = sheet.Cells(i, 1).text
            Row.StateTime = sheet.Cells(i, 2).value
            Row.FailureMode = sheet.Cells(i, 3).text

          'Add the current row to the data collection. 
           Call DataCollection.AddDataRow(Row)
       Next i

      'Close the Excel file. 
       Workbook.Close()
       Excel.Quit()

      'Connect to the Synthesis repository. The following code assumes that 
      'a standard repository called "RSRepository1.rsr10" exists in the C: drive.   
       Dim MyRepository As New Repository   
       MyRepository.ConnectToRepository ("C:\RSRepository1.rsr10")

      'Save the extracted data collection to the repository. 
       MyRepository.DataWarehouse.SaveRawDataSet(DataCollection)

     End Sub
End Class

References

To learn more, see the reference documentation for the classes and methods discussed in this section: