You are on page 1of 21

PCES presents:

LabView
Database Access

Presented by
Jon Gray

PCES presents:

LabView
Database Access

Integrating MS SQL and other


databases into LabView programs

Contents

Before programming

Introduction to NI provided .VIs

Connecting through:
Data Sources
Descriptor files
Connection strings
Using multiple methods to help development

How to avoid SQL

Table design

SQL command examples

Real design planning and development

Where to find help on SQL, table design, etc.

LabView
Database Access

Before You Start


Install a local copy of MS-SQL for experimenting
Install a copy with tools. The file will have WT in the name. The tools
include SQL Server Management Studio, which is a much easier way to add
and configure tables and to experiment with SQL commands.
Create tables using the SQL Server Management Studio.

LabView
Database Access

Introducing LabView .VIs


Open & Close Connections always required
Open: Required before any other access. More details will be presented later.
Close: Required after all accesses. Failing to Close repeatedly will eventually crash
the database engine, and may block other access.

LabView
Database Access

Introducing LabView .VIs


Simple accessors most common method

Select: Read selected columns from selected rows of a single table. Selecting rows
requires some understanding of SQLs WHERE clause. The Select Data.vi can be
used for reading combinations of tables, but that requires more understanding of
SQL.
Database Variant To Data: Needed to convert data to LabView data types.
Insert: Add new rows into a table. No SQL needed, but new data must match the
column types and size. Strings or numbers too large to fit will cause an error.
Update: Changes an existing row in a table. Requires enough understanding of
SQLs WHERE clause to select a (usually) single row. New data must match the
column types and size. Strings or numbers too large to fit will cause an error.
Delete: (not shown) Removes rows from a table. Requires enough understanding
of SQLs WHERE clause to select a (usually) single row.

LabView
Database Access

Introducing LabView .VIs


SQL Command

Executes any SQL command or query.


Data may be fetched one row at a time. This is helpful if data may be returned
slowly, and the program needs to process or display the first records before the last
records arrive.
Must use DB Tools Free Object.vi. The reference type between .vis helps encourage
this.

LabView
Database Access

Introducing LabView .VIs


Database Variant To Data

Convert each row to a cluster. Columns fetched must be in the same order as
members of the cluster.
GOTCHA: NULL strings convert to . NULL numbers convert to 0. But NULL dates
convert to 1/1/1904, and prevent all following values from being converted. If dates may be
NULL, convert them to string then to date, or convert them outside the cluster.

LabView
Database Access

Connecting to a Database
DB Tools Open Connection.vi
The connection information, by default, expects a
file path. A string also works.
There are 3 good inputs to use:
1. Path to a .UDL file or .DSN file (as path).
2. Name of ODBC Data Source (as string).
3. Connection string (as string).

Any method requires authentication (to any SQL


server). The authentication can be:
1. SQL login. These must be added and
managed within the SQL server.
2. Windows login. These must also be listed in
SQL server, but Windows handles
passwords.

LabView
Database Access

Connecting to a Database
Path to .UDL file
Created by LabView.
From any window, use menu Tools/Create Data
Link
For .exe builds, the file must be managed correctly.

LabView
Database Access

Connecting to a Database
Path to .DSN file
Created by Windows ODBC Data Source
Administrator, in the File DSN tab.
GOTCHA: There are two ODBC Data Source
Administrator programs. You must use the one
designed for 32-bit software (LabView database
access is 32-bit). This version is
C:\Windows\SysWOW64\odbcad32.exe, and
does not appear in any Control Panel windows.
For .exe builds, the file must be managed correctly.

LabView
Database Access

Connecting to a Database
Name of ODBC Data Source
Created by Windows ODBC Data Source
Administrator in the User DSN or System DSN tab.
GOTCHA: There are two ODBC Data Source
Administrator programs. You must use the one
designed for 32-bit software (LabView database
access is 32-bit). This version is
C:\Windows\SysWOW64\odbcad32.exe, and
does not appear in any Control Panel windows.
Not easy to deploy with a program installer. Not
easy to configure for most users.
Great for development and testing.

LabView
Database Access

Connecting to a Database
Connection String
Create a .UDL using LabViews Tools/Create Data
Link
Use simplest provider (i.e. SQL Server instead of
SQL Server Native Client 10.0).
Open the .UDL with notepad (it may help to change
.udl to .txt first).
Copy text into a string and pass to DB Tools Open
Connection.vi.
For example:
Provider=SQLOLEDB;Persist Security Info=False;
User ID=sa;Password=My#Password1';
Initial Catalog=tempdb;Data Source=PCES-68-LV2013

Password and User ID may be include in the string,


or passed to DB Tools Open Connection.vi
separately. Do not pass User ID or Password when
using Windows Login.
LabView
Database Access

Connecting to a Database
Multiple Methods
Recommended to simplify testing and
development.
Many methods possible. Recommend:
Connect with an ODBC Data Source. The
data source is configured manually, and only
on the developers computer. This data
source is connected to a duplicate of the real
database. Attempts to use a missing ODBC
Data Source fail very quickly.
Then connect with a Connection String. The
connection string is for the real database.

LabView
Database Access

Avoiding SQL
Use short-cut .vis from LabView.
Avoid NULL. Set all columns to not allow NULL.
You will still need to understand WHERE clauses.
Example:
WHERE FirstName=Jon AND Employer=PCES AND (LastName=Gray OR Hire<2009-04-01)

WHERE is usually required. It starts the condition for each row.


AND and OR combine requirements. NOT reverses a condition after it. (do not
use Employer NOT = PCES, instead use NOT Employer=PCES or
Employer<>PCES)
Parenthesis work like in algebra.
Strings require half-quotes. Dates must be full dates or date and time. LabViews
string conversions from date work well; use %T.
Condition inputs can also accept ORDER BY and GROUP BY clauses.

LabView
Database Access

Table Design
8 hours on table design can easily save 20 hours on changes later.
Tables should be designed with several key details in mind. These details
should be determined before design, as much and as accurately as possible.
Volume of data expected.
All read accesses needed.
All write accesses needed.
Security.
Expected changes in the future.

LabView
Database Access

Table Design
General tips:
Avoid NULL values where possible. Each column in a table may be configured to allow NULL
instead of a value. NULL values are more difficult to manage in LabView.
Keep number of columns in a table small, where possible.
If a group of columns is commonly unused for rows of a table, consider moving those columns
to a separate table. For example, if an Employee table only uses CommissionRate,
SalesCount, and CommissionTotal for employees who are in sales, it may be better to make a
separate table for SalesEmployee, with EmployeeID, CommissionRate, etc. Only employees in
sales would have a record in SalesEmployee.
Each table should have a Key, where possible, such as EmployeeID. Numeric keys are best.
Avoid SQL enum types for columns. They work, but its easier to use a number, and let
LabView automatically convert the number to a LabView enum.
Conventionally, table names are capitalized, and singular, such as Employee, not employees.
Columns are commonly capitalized, but some developers start with lower-case. No name
should be all-caps.
Avoid spaces and SQL words like DESC for columns. You would need to access it as [Desc].
Auto-increment in SQL is good, but difficult to handle with LabView. Use it with caution.

LabView
Database Access

Table Design, Example


Person
PersonID
FirstName
LastName
FavoriteBookID

Book
BookID
Title
ISBN
Author

PersonBook
PersonID
BookID
Finished
Rating

Comment
PersonID
BookID
Comment

There may be any list of persons and books. They are separate tables because a Book isnt affected by entries in
Person, and a Person isnt affected by entries in Book, even though they might have a relationship.

Each PersonBook record references one Person and one Book, and adds a little information. Only one record should
have a particular PersonID and BookID, but the SQL database wont enforce that easily. PersonBook is a linking
table.

Each Comment record also references one Person and one Book, but one Person may make several comments
about the same Book. Comment is a linking table because it links two other tables, but is also an attribute table
because it adds an arbitrary list of new attributes to rows in another table (in this case, 2 tables).

In Database terminology, the link Person:PersonBook is 1:N. That is, one Person may have many matching
PersonBook records. Other options are 1:1, 1:0/1, or 1:1+, meaning that one record in the first table is tied to 1 record
in the other, or optionally to 1, or to at least one. The relationship can also be M:N, where any number from either
table can reference the others, but this is less useful.

There may have been a temptation to mix PersonBook with Comment. But if most people have no Comment, and
some have more than one, they need to be separated. If you may someday add a Comment Type, then separate
tables would make the change easier.

LabView
Database Access

SQL Statement Examples


Person
PersonID
FirstName
LastName
FavoriteBookID

Book

PersonBook

BookID
Title
ISBN
Author

Comment

PersonID
BookID
Finished
Rating

PersonID
BookID
Comment

SELECT FirstName, LastName, read, Title FROM Person,Book,PersonBook WHERE


Person.PersonID=PersonBook.PersonID AND Book.BookID=PersonBook.BookID

Can be done with LabViews DB Tools Select Data.vi. The table name is Person,Book,PersonBook, and the
columns include read. The columns table to DB Tools Select Data.vi does allow functions and constants.

Because FirstName is not in Book or PersonBook, it does not need to be written as Person.FirstName. But
because PersonID is in two tables, it must have the table name included whenever it is used.

This is sometimes called an implicit CROSS JOIN or NATURAL JOIN, or equi-join. An INNER JOIN does
the same.

SELECT FirstName, LastName, rated, COUNT(BookID), books as , Rating FROM Person,PersonBook WHERE
Person.PersonID=PersonBook.PersonID GROUP BY Person.PersonID, Rating

Can also be done with DB Tools Select Data.vi.

Can also include ORDER BY clause.

OUTER JOIN can be used to show persons who read particular books and their comments, even if they didnt leave
any comments. Or it can be used to list what books each person read, or NULL if they didnt read any.

Experiment in MS SQL Management Studio.

LabView
Database Access

Recommended Further Reading


SQL Syntax
INNER JOIN and OUTER JOIN, etc.
SELECT FROM Table1,Table2
ORDER BY
GROUP BY
More SQL
Stored Procedures
Replication
Security
Change Tracking

LabView
Database Access

Where to Answer Questions


Google: Include ms sql in the search. Best answers are often from:
https://msdn.microsoft.com/en-us/library... (official documentation)
https://technet.microsoft.com/... (user forums)
www.w3schools.com (great for mysql and other SQL servers)
stackoverflow.com (best for other SQL servers)
MS SQL Management Studio / Help
LabView / Help / LabView Help
LabView / Help / Find Examples

LabView
Database Access

You might also like