Professional Documents
Culture Documents
Connectivity Toolkit
The LabVIEW Database Connectivity Toolkit allows you to quickly connect to
local and remote databases and perform many common database operations
without having to know structured query language (SQL) programming. This
toolkit greatly simplifies the process of making fast and repeated calls to popular
databases, such as Microsoft Access, SQL Server, and Oracle. If you need
advanced database functionality and flexibility, the Database Connectivity
Toolkit also offers complete SQL capabilities.
Database Terminology
SQL (Structured Query Language) is a set of character string commands
that is a widely supported standard for database access. SQL statements
allow you to use a common set of commands to access different databases.
SQL statements can be used to store data into a database, query a database
for records that match certain criteria, and many other database operations.
At its most basic level, the Database Connectivity Toolkit uses SQL
statements to access, modify, and view information in databases. These
statements are constructed in the toolkit’s VIs and then executed through a
Microsoft technology, OLE DB (Object Linking and Embedding Database),
that provides a connection between LabVIEW and the database.
Inside LabVIEW, property and invoke Nodes make calls to the Microsoft
ActiveX Data Objects (ADO). ADO will then connect to the database
through either a Data Source Name or Universal Data Link.
With OLE DB you can communicate with any DBMS that supplies an
ODBC driver or OLE DB provider. OLE DB uses the OLE DB Provider for
ODBC as a conversion layer between OLE DB and ODBC if an ODBC
driver is used to communicate with a database. However, there are native
OLE DB Providers for different DBMS such as SQL Server, Jet (Microsoft
Access), and Oracle. These native OLE DB providers are more efficient
than the OLE DB Provider for ODBC because they remove the need to
convert from OLE DB to ODBC and then to the DBMS.
2
The Database Programming Model
The Database Connectivity Toolkit has three “major” palettes of VIs, all
located in the All Functions » Database palette.
Database VIs
The High Level VIs in the Database Connectivity Toolkit allow access to
the database without the use of any SQL statements. There are VIs to open
and close connections to the database, insert and delete tables, insert and
select records, and convert variants. All data stored in a database is passed
using the Variant data type in LabVIEW.
Advanced VIs
The Advanced Palette gives you more flexibility in searching, modifying,
and viewing data from a database. The Advanced VIs allow you to execute
SQL queries into databases. They also allow you to select and navigate
through recordsets in the database. Furthermore, these VIs expose some of
the more advanced features of the Database Connectivity Toolkit such as
parameterized queries.
3
Figure 1.2 – Advanced Database Operations
Utility VIs
The Utility VIs are used to get and set connection and general information
about the database. Using the Utility VIs you can view the names of the
tables in a database, and the fields (columns) in a table. You can also view
and set different connection parameters for the database such as connection
string and connection timeout. In the Utility VIs, there are also VIs to store
recordsets to file and load them back in to LabVIEW from a file.
This basic programming model is shown in Figure 1.4. Each of the three
steps are discussed briefly below. Additional information, along with hands-
on exercises, is provided in the next few sections.
4
VI will accept a path to a File DSN or UDL, or the name of System or User
DSNs. If a connection has not yet been established to a database, one can
be created by setting the optional prompt terminal to TRUE.
If you want to select data from the database, use the DB Tools Select Data
VI to select records. To use this VI, you first need to wire in a connection
reference and the name of the table that you want to extract the
information from. You can also specify specific columns to extract. The
DB Tools Select Data VI will return all of the fields from all of the records
that are in the columns supplied and meet the optional clause. The optional
clause is an SQL statement. If no SQL statement is wired into the optional
clause it will extract the data from all of the records. If you want to extract
all of the fields of all the records, then simply leave the optional clause and
columns terminals empty. The data will be returned through the data
terminal. It will be returned as a two-dimensional array of variants. Each
row of the array is a different record and each column is a different field in
the record. Use the Database Variant To Data VI to convert from the
variant to the correct data type.
5
Step 3: Close Connection
After you have completed all of the operations on the database, close the
connection to the database. This operation is done with the DB Tools Close
Connection VI. Once a connection to a database has been closed, no more
operations can be done on that database from within LabVIEW until a new
connection is established with a DB Tools Open Connection VI.
6
2
Connecting to a Database
The first step in doing database operations with the Database Connectivity
Toolkit is to connect to the database. There are two major types of
connections that can be created for LabVIEW to communicate with a
database - Data Source Names (DSNs) and Universal Data Links (UDLs).
DSNs are used to facilitate ODBC communication, and UDLs are used for
OLE DB connections between the DBMS and LabVIEW.
In the next exercise you will first create a blank Microsoft Access database,
and then create two different connections to that database - a system DSN
and a UDL.
7
Part 1. Create a Database
In this secion you will create a simple database that will be used for the rest
of the parts of the exercise.
3. Save the database and close Microsoft Access. You will add tables and
data to the database using the Database Connectivity Toolkit in
Exercise 2.
8
Figure 2.2 - ODBC Data Source Administrator
9
Figure 2.3 - Create New Data Source Window
10
6. Select OK to finish creating the DSN.
11
4. On the Connection tab, browse to and select the Test Results.mdb
database you created on the desktop in Step 1. Click the Test
Connection button to test the UDL connection.
12
3
Inserting Data in to a Database
Once the connection to the database is established, you can then use the
Database Connectivity VIs in LabVIEW to insert data in to the database.
In the next exercise you will use LabVIEW to create a table in a database
and insert data into the table. Use the UDL created in Exercise 2-1 to
communicate with the database.
13
terminal. Wire the error output to the shift register on the For
Loop.
b. Place a DB Tools Insert Data VI (All Functions » Database)
inside the For Loop, and make the following connections:
i. Wire the connection reference from the DB Tools Create
Connection VI to the connection reference input terminal.
ii. Wire the output cluster from the Bundle function into the
data input.
iii. Wire the boolean constant in the True state to the create table
input. This ensures that the table is created in the database if it
does not already exist.
iv. Wire the Table Name string control to the table terminal.
This is the name of the table that you are going to create and
insert the data into.
v. Wire the Column Names array of String controls into the
columns terminal. These names are the names of the columns
you will add into the table and then insert the data into.
vi. Wire the error out to the shift register on the For Loop.
c. Place a DB Tools Close Connection VI (All Functions »
Database) outside of the For Loop and wire the connection
reference from the DB Tools Insert Data VI output terminal to
the connection reference input terminal. Be sure to disable auto-
indexing if it is enabled. Wire the error from the Shift Register to
the error in input terminal.
d. Place a Simple Error Handler VI (All Functions » Timing and
Dialog) at the end to display errors that may occur during the
program. Wire the error out from the DB Tools Close
Connection VI into the error in terminal of the Simple Error
Handler.
3. Run the VI once. This will create the ManufacturingTests table in the
Test Results database. It will also add six records into the table
corresponding to the tests run on the UUTs.
4. Save the VI to the Desktop as Insert Test Results.vi.
5. View the updated Test Results Database in Microsoft Access.
a. Open Microsoft Access and select File » Open to open the Test
Results database located on the Desktop.
b. Double-click on the ManufacturingTests table in the Test
Results database to open the table and view the stored data. The
table should appear similar to Figure 3.2.
14
In this exercise, the Database Connectivity Toolkit VIs allowed you to
quickly and easily create a table in a Microsoft Access database and insert
data in to that table, all from the LabVIEW environment.
15
4
Selecting Data from a Database
Another common database operation is selecting data from a database.
In the next exercise you will use the high-level Select Data From
Database VI to select data from a database.
16
a second wire branch and wire the data to the For Loop for
extraction in the loop.
c. Place a DB Tools Close Connection (All Functions » Database)
VI outside of the For Loop and wire the connection reference and
error out from the DB Tools Select Data VI output terminals to
the connection reference and error in input terminals of the DB
Tools Close Connection VI. Also wire the error out to the shift
register on the For Loop.
d. Inside the For Loop, wire the data variant array to the array input
of the Index Array.
e. Place three Database Variant To Data functions (All Functions »
Database) inside the For Loop. Wire the error clusters together to
ensure data flow and to check for errors.
i. Wire the first element of the Index Array to the
Database Variant terminal of the first Database
Variant To Data function. Wire a string constant to
the type terminal. This will ensure that the variant
data type is converted to a string.
ii. Wire the second element of the Index Array to the
Database Variant terminal of the second Database
Variant To Data function. Wire the numeric
constant to the type terminal to specify the variant
data type as a numeric double.
iii. Wire the third element of the Index Array to the
Database Variant terminal. Wire the remaining
string constant to the type terminal.
f. Wire the error out from the final Database Variant to Data
function to the Shift Register. Wire the data output terminals of
each function to the edge of the For Loop. Ensure that each has
Auto-Indexing enabled, and then wire the data to their
corresponding indicators - Unit Under Test, Test Number, and
Result respectively.
g. Place a Simple Error Handler VI at the end to display errors that
may occur during the program. Wire the output from the Shift
Register to the error in terminal of the Simple Error Handler.
3. Run the VI. The VI will extract all of the data from the database and
display it in the appropriate indicators, as Figure 4.2 illustrates.
17
Figure 4.2 - Exercise 4-1 Front Panel After Running VI
In this exercise, you selected and displayed data from an Access database
using the Database Connectivity Toolkit VIs.
18
5
Executing SQL Queries
To execute an SQL statement and navigate through the returned data, you can
use the Advanced VIs in the Database Connectivity Toolkit. To execute a query,
use the DB Tools Execute Query VI (All Functions » Database » Advanced).
This VI accepts an SQL query string, which is an SQL query, and it returns a
recordset reference. The recordset reference is a recordset corresponding to all
of the records that matched the query parameters.
Once you have a reference to the matching data, you can fetch data from the
recordset in a variety of forms. You can extract all of the data at one time using
the DB Tools Fetch Recordset Data VI, or you can navigate though individual
records or fields using the other Advanced VIs.
After the data has been fetched from the recordset, close the reference to the
recordset using the DB Tools Free Object VI.
In the SQL Query example in Figure 5.1, all of the fields for records in the
testdata table that have true in their col4 field are fetched from the database.
19
6
Summary
Congratulations! During this short tutorial you have successfully accomplished
the following tasks using some of the functionality included in the LabVIEW
Database Connectivity Toolkit:
Now that you are familiar with some of the functionality included in the
Database Connectivity Toolkit, please feel free to continue exploring the
numerous other features both LabVIEW and this toolkit have to offer.
20