Nova Explorer and MySql scripting sample - Part III

by Frédéric Colin 9. November 2009 09:06

Enough words. It is time to code! I suppose you installed all the products described in this post.

In terms of functionalities, I decided to load data from database once when the script module is executed. The other solution would have been to load only the needed data from database, i.e., one request for each pickable object with data to display. In order to reduce requests on the database server, I implemented the first solution.

So, the first thing to do is to declare the connection string to the database:

Private ConnectionString As String
= "Driver={MySQL ODBC 5.1 Driver};
Server=192.168.0.103;Port=3306; Database=bCatalog;User=root;Password=bewise31;Option=3"

You will notice that I used the Super Administrator account to connect to the database. Of course, it's bad, and you should have a specific account with only the needed rights.

I decided to store database records in a generic list of Products, to keep them in memory: 

The next step was to access to the database to fill the previous generic list:

DatabaseProducts is the generic List of Products. The reader is a kind of record set read only forward only where you retrieve values from the database, record by record. The database connection must still be opened while reading values. In VB.NET, the syntax "Using ... End Using" is very useful to automatically free the memory by calling Dispose method on the object connection in my sample.

It's now time to talk about the BuildDataPanel method used previously which contains the "intelligent" part of the script that will generate on the fly the needed user interaction to display the data panel:

The first thing to do in this code is to find the corresponding object in the scene. For this, I use the generic list Find method that admits a predicate (in fact a delegate) as a parameter. Please, don't focus on the trick I use to filter records to pass a parameter value corresponding to the filter. The Searcher class is used for that and it just works. Once done, I make the selected object pickable to allow interactions on it.  You will also notice the use of a "DefaultCultureInfo" member that is a CultureInfo instance, set for the French culture to format a decimal in currency (euro).

The next thing is to create the trigger as a user interaction (user click) to attach it the Nova Scene object found previously. Then, on this trigger, we will add a custom action as a Nova static text. It's the widget used to display data within the scene. The next couple lines of code are here to set the action (text to display, visibility duration, colors, etc.)

The last step is to call the Reset Method on the created trigger to initialize the behavior and that's it!

I'm quite sure you will be more interested in the sample code source than my explanation :-). That's why in the following zip, you will find:

  • the mxb file I used,
  • the corresponding nss file,
  • the nsr file that contains the previous scripting in case,
  • MySql database scripting.

    VerticeSample.zip (38.21 kb)

Enjoy!

To be continued ...

 

Comments are closed
Disclaimer
The opinions expressed herein are the author own personal opinions and do not represent their employers' view in anyway..

© Copyright 2010 Nova by Vertice Team