Database Communication in LabVIEW

Hans-Petter Halvorsen

What is LabVIEW?

LabVIEW is a graphical programming language, and it has powerful features for simulation, control and DAQ applications.

You can also use LabVIEW to communicate with databases.

LabVIEW SQL Toolkit (YouTube) + PowerPoint (PDF)

LabVIEW SQL Toolkit

I have made a LabVIEW SQL Toolkit, which makes it easy to communicate with a database from LabVIEW.

 

Below you can dowload the LabVIEW SQL Toolkit and use it for free:

Download LabVIEW SQL Toolkit for LabVIEW

Follow the installation instructions in the ReadMe.txt file inside the Zip file. Note that LabVIEW 32 bits edition is typicaly installed in the following folder: "C:\Program Files (x86)\National Instruments\LabVIEW XXXX", where XXXX is the version number of LabVIEW.

Below we see a practical example:

In the example above, an ODBC connection has been configured in advance.

ODBC:

ODBC (Open Database Connectivity) is a standardized interface (API) for accessing the database from a client. You can use this standard to communicate with databases from different vendors, such as Oracle, SQL Server, etc. The designers of ODBC aimed to make it independent of programming languages, database systems, and operating systems.

In Windows 10 you find the "ODBC Data Source Administrator" tool here: Control Panel → Administrative Tools → Data Sources (ODBC). Then click the "Add..." button in order to create an ODBC connection to your database.

Note! Make sure to use the 32 bit version of the ODBC Tool!

 

Connection String:

An alternative to ODBC is to type directly type your Connection String for your Database. The Connection String looks like this (just replace the text MyODBC with the text below):

PROVIDER=SQLOLEDB;DATA SOURCE=<SQL Server Name>;DATABASE=<Database Name>;UID=sa;PWD=<Your Password>;

Replace <SQL Server Name> with the name of your SQL Server, typically "<YourComputerName>\SQLEXPRESS" if you are using SQL Server Express.

UID is a SQL Server user, here you can create your own SQL Server user inside SQL Server Management Studio or use the built-in sa user (sa = System Administrator). During the setup of SQL Server you need to select "Mixed Mode" and enter the password for your sa user.

It may look something like this:

PROVIDER=SQLOLEDB;DATA SOURCE=DELLPCWORK\SQLEXPRESS;DATABASE=MEASUREMENTS;UID=sa;PWD=Password123;

 

LabVIEW Database Logging Example

Here we will create a simple Database used for Logging in SQL Server and a basic LabVIEW Application for Logging Data from LabVIEW into SQL Server.

In this example the SQL Server function getdate() is used. This function just makes sure that the current timestamp is inserted into the "TagTimeStamp" database column.

Decimal Numbers in SQL:

Using Decimal Numbers in SQL can cause problems. Assume we want to insert the Value "22,5":

insert into TAGDATA (TagValue, TagTimestamp) values (22,5, getdate())

Here, SQL assumes you try to insert 3 values into 2 columns since SQL uses "," as a separation symbol, and you will get an error running this statement.

If we instead wite it like this:

insert into TAGDATA (TagValue, TagTimestamp) values (22.5, getdate())

Then it will work without problems.

 

Format Into String:

Using %.; in front of the string means that "." will be used as Decimal Point. Then we will avoid the decimal problem illustrated above.

%2.1f means that this is replaced with the value that comes from the Temperature control with one decimal value. “f“ means it is a floating point value

 

Resources:

LabVIEW Database Logging Tutorial

LabVIEW Database Logging Code Example

Here you see an improved version:

Datalogging in LabVIEW with SQL Server (YouTube) + PowerPoint (PDF) - This Tutorial takes the basic LabVIEW Datalogging Example shown and Adding Value by using a State Machine, retrieves available Sensors from the Database, stores the Database Connection in a File, etc. Take a look to get some good ideas. You can also download the LabVIEW Source Code.

 

LabVIEW Resources

Below you will find more interesting LabVIEW resources


LabVIEW Fundamentals

Learn Basic LabVIEW Programming

LabVIEW DAQ

Learn LabVIEW DAQ Programming



LabVIEW LINX

Learn LabVIEW LINX

Python

Learn Python Programming