LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing data to Microsoft SQL Server/Database

Hi All
 
I have written a program in LV ver 6.1 that carries out various functional tests and at each test gathers 3 data values. At the moment the data is gathered at each stage of the test and written in a concatenated string to a file that was opened at the start of the test routine. What I need to do is output the data such that it is compatable with SQL server (presumably in some form of table?). I have read previous posts but they recommed the ni database toolkit, which I do not have. Should I be using the XML vi to take the string of data or use the active x vi? I have read in other posts that you can manage without the ni database toolkit but would need to program a database structure. My knowledge of database development is nil and I wondered if there was a vi that could be used that would create tables that could be directly interpreted by MS SQL?
Labview Version 8.5
Labview Version 8.6
Labview Version 2014
0 Kudos
Message 1 of 12
(7,097 Views)

Do you need to put the data in a specific DB or just allow the DB to access it? An application built using SQL server should be able to read a standard ASCII file.

If you do need to save it into a specific DB, then you probably need to have a table for the data (you can create it using SQL server or Access or by using an SQL command from LV) and insert that data into the table using a simple SQL command. You should be able to do this with LabSQL. Depending on the nature of your system, it might get more complicated.

If the DB exists and is managed by someone else, you should talk to them. If it doesn't, you should consider whether you need it at all, but if you do, you should start by reading some tutorials on DBs.


___________________
Try to take over the world!
Message 2 of 12
(7,082 Views)

(Do you need to put the data in a specific DB or just allow the DB to access it?) 

Once the file containing the saved data is closed it needs to be sent to the database which is Microsoft SQL server. It is highly unlikely that my Labview program would ever have to read the database or use any values from it as it only controls a test and calibration rig.

(An application built using SQL server should be able to read a standard ASCII file)

Does this mean I could just save my data that was created as strings (originally intended to go into a spreadsheet for analysis) to some location on my clients server and he could then recover the data and manipulate it into tables to suit his requirements?

I have had a look at some tutorials that were linked on the forum a few years ago and they were very good (simple explanations!) and described the control words used to create and access a database. But with limited knowledge I am concerned that I could end up spending considerable time trying to work with a database whilst the original control/test aspect of the project goes pear shaped!!

From your reply my solution might be straightforward?

Thanks 

Labview Version 8.5
Labview Version 8.6
Labview Version 2014
0 Kudos
Message 3 of 12
(7,062 Views)

Again, the question is whether this DB already exists or whether you're expected to create a new DB.

Assuming that it already exists, talk to the person managing the DB. If they can read an ASCII file, then you can just put that file on the server and let them manage it.

If they can't, then you will need to form a connection to the database (in Control Panel>>Administrative Tools>>ODBC Data Sources) and insert the data into a table. You should probably define a specific table just for that and simply push your data into that table.


___________________
Try to take over the world!
0 Kudos
Message 4 of 12
(7,060 Views)
With SQL Server, a text file, Excel spreadsheet, xml file, etc. can be easily imported into an existing table. The problem is that it would be an extra pain-in-the-a** step for the database user and the user would probably grumble. To directly write your information to an existing table is actually pretty simple. Imho, it is just as simple to do that as to create a large text file and much simpler than using ActiveX to write to Excel. You say you only have 3 values to save? Then the design of the table is easy and you could probably get the assistance of the database user. It's just a matter of deciding the format of the data for each column (numeric, string, date/time. etc.). If you use the database toolkit from NI, then I believe that all it takes is a single subVI (maybe 2) to write. If you use the free LabSQL (http://www.jeffreytravis.com/lost/labsql.html), then after connecting to the database, there is a single subVI to do the data insertion. The SQL command syntax is "INSERT table_name (col_name, col_name,...) VALUES (expr, expr, expr...)". So, if you had a table called results and numeric columns called meas1, meas2, meas2, and values of 1,2,3 to save, the command would be "INSERT results (meas1, meas2, meas3) VALUES (1,2,3)". The command is easily created with the Format Into String Function.
Message 5 of 12
(7,052 Views)
My client already has Microsoft SQL server database in place, which is used for stock control and manufactuing analysis but I do not believe it handles data obtained from testing and control equipment (yet!) as most of that type of kit has been supplied by myself over the last few years and just saves data locally in spreadsheet format.
 
 Following reply from tst and my desire to duck this one!, I suggested  that they just have the file output 'as it is' in spreadsheet format, but as you mention Dennis, they were not very keen on that approach. I guess the main problem being is my contact with the company is the guy who manages their computer and manufacturing operations, so it would be more grief for him!
 
I have suggested the option of purchasing the ni database toolkit, but will try LabSQL first and see if I can get that to work.
 
Sorry if it looks as if I am going round a bit with this problem but as ever its all down to development time and costs.
Labview Version 8.5
Labview Version 8.6
Labview Version 2014
0 Kudos
Message 6 of 12
(7,041 Views)

Pity the poor IS person who actually has to do some support.Smiley Wink It may be more work for him but that's what he is there for and it sounds like it's his company that wants the test data integrated into the database. You could probably leave a lot of the details up to him and let him decide on the basic design. He will need to contact the users and find out how they wish to use the data. For example, your test results may need to link to shipping. Besides the test results, you might also need to store the serial number. The date/time of the test is usually a requirement as well. The IS person is (or should be) capable of writing a stored procedure that makes it a bit easier for you as well. A stored procedure is basically a program that will accept parameters and do all of the low level database commands for you.

Since the database is at the client's and you may not have access at all times, you might want to consider setting up a local MS Access database with a simple table that would save just your results. This would allow you to get familiar with either the database toolkit or LaBSQL and do all of the debug locally. When it's time to install the program at the client, you just have to change the name of the database server and this can be done externanly from LabVIEW in a .udl file or from the windows ODBC administrator.

Message 7 of 12
(7,037 Views)
Full Dev suite finally arrived with the database toolkit in time for Christmass, yipee!! But then I got to reading the LV user manual and some texts on SQL and relational databases, things got worse and inverse learning took place!
 
Summerising what I think that I have understood from advice given and my grasp of things,
 
Ideally code written for database connectivity should not be included in my Labview code or problems could occur if my client changed his servers , database structure etc.
 
Microsoft SQL server is an intermediate piece of software that allows my Labview code to access the database without actually 'seeing' it. With my client using SQL server they can modify the way my Labview code sends data to their database if they decide to change their servers, database structure etc.
 
ADO connectivity is used and the connection string is best located in the windows registry file. Presumably this option would give my client the most flexiblity? Is this the same thing as the .udl file?
 
It is more efficient to use the Parameterized database technique such as the example given in the 'logging example parameterized'
 
I have sent my client an outline of the data to be saved on their database, which at the moment comprises of 3 tables, the first has things like operators name, date, time, serial number etc ( 7 records, 3 fields) the second table has test reference (11 tests in total) with items such as fuel flow, fuel temp etc (11 records, 5 fields), Last table has 1 record and 6 fields. Unlike the Labview example each stage of the test can take up to 4 minutes to complete and if successfull the program moves on to the next stage of the test, saving the test results and if a PASS or FAIL occurred. The question I have is would it more efficient to save the results at the end of all of the tests or shold I connect to their database and save results as the tests progress?
 
Sorry if this post is long winded but have kept putting it off and playing with the PID stuff instead (must be desperate!!)
 
Labview Version 8.5
Labview Version 8.6
Labview Version 2014
0 Kudos
Message 8 of 12
(6,923 Views)

I'm having trouble understanding exactly what you're doing, but I will try to explain a few things:

Ideally code written for database connectivity should not be included in my Labview code or problems could occur if my client changed his servers , database structure etc.

The code can be yours. If you do write it, just make sure to create subVIs, so that if the DB structure does change (something which is rare), you will only need to change the code inside the subVIs.

Microsoft SQL server is an intermediate piece of software that allows my Labview code to access the database without actually 'seeing' it. With my client using SQL server they can modify the way my Labview code sends data to their database if they decide to change their servers, database structure etc.

SQL Server is the DB management system. It holds all the tables, queries, etc. and is responsible for actually managing the DB itself.
What Dennis suggested was that the DB guy will write something known as a "stored procedure" and that you will call it.
In such a case, if the DB guy wanted to change something, he could just change the stored procedure and (if the change does not affect the interface), you do not have to change or rebuild your program. This is equivalent to the subVIs I described earlier, except in this case, the function is inside the DB itself.

Another version of this is creating a table with SQL commands and assigning a name for each command. You load the SQL command from the table by name and execute it. This allows for changing the SQL command inside the DB without changing your application.

ADO connectivity is used and the connection string is best located in the windows registry file. Presumably this option would give my client the most flexiblity? Is this the same thing as the .udl file?

Basically, the scheme is like this:

DB <-> DBMS <-> ODBC <-> ADO <-> Your Application

This may not be perfectly accurate, but the concept is basically this - in Windows, you talk to ANY database from ANY program using the same interface - you call ADO functions which will be responsible for getting to the DB you want and you use SQL to issue the commands, since the basic SQL commands are supported by all DBs.

To identify the DB, you use a connection string. This connection string can have all the data in it or it can point to an existing file (UDL or DSN) which holds the data pointing to the DB. Dennis suggested that when developing on your machine, you will use a file pointing to a local DB, but when deploying you will use a file of the same name which will point to the real DB. Your program will always go to the file and from there it will know which DB to go to.

The only time when this should really matter to the client is during configuration (for example, if the DB was moved to a different machine). All the client needs to do is edit the UDL file to point at the new location of the DB.

Again, the advantage of all this is that this is not part of your code and is done by programs which are built to handle it.

It is more efficient to use the Parameterized database technique such as the example given in the 'logging example parameterized'
I don't know how much this efficiency will be relevant in the speeds you need.
In any case, as I said, my own preference is to create functional subVIs - for example, a VI which will accept data in my format and will be responsible internally for converting that data and putting it into the necessary tables.

I have sent my client an outline of the data to be saved on their database, which at the moment comprises of 3 tables, the first has things like operators name, date, time, serial number etc ( 7 records, 3 fields) the second table has test reference (11 tests in total) with items such as fuel flow, fuel temp etc (11 records, 5 fields), Last table has 1 record and 6 fields. Unlike the Labview example each stage of the test can take up to 4 minutes to complete and if successfull the program moves on to the next stage of the test, saving the test results and if a PASS or FAIL occurred. The question I have is would it more efficient to save the results at the end of all of the tests or shold I connect to their database and save results as the tests progress?

I can't say I understand this, but my suggestion would be to let the client decide in what format they want the data, since they're the ones who want it. Work with the DB guy to decide exactly what data you need to put into the DB and where and then you can decide exactly which functions you need. As said, you might want to let the DB guy write some stored procedures which you can use to input the data.

Unless it's a large amount of data, writing to a DB is usually fairly quick (under a second). If it is relevant, my suggestion would be to write after each phase.

In any case, I suggest you go through some real DB tutorials (not LV related) and see if you can wrap your head about it. I know I don't particularly like designing DBs (or PID) any more than you do, but it's a skill worth having, and in some places, a DB makes things easy and practical.


___________________
Try to take over the world!
Message 9 of 12
(6,901 Views)
Thanks for advice tst.
 
The difficulty I have with modifications to the program, once delivered to my clients, is the physical distance between us, as I am based in France and they are in the UK. So as far as possible I want any interface to their computer system to be done in such a way that they can modify it as far as possible. They would not have access to my Labview code for any changes they may want to carry out so the 'stored proceedure' or pointing to the database where my client can change the UDL file seems like the best solution.
 
I have ordered some books as suggested on Database design as I think that you are right and it will pay in the long run as my client is talking about having all of their control software changed from Agilent VEE to Labview, fingers crossed!
 
Thanks again and have a good New Year
 
John
 
 
Labview Version 8.5
Labview Version 8.6
Labview Version 2014
0 Kudos
Message 10 of 12
(6,859 Views)