Sie sind auf Seite 1von 2

10/10/2017 62191 - Connect to SQL 2005 Express Database Using ADO

Connect to SQL 2005 Express Database Using ADO


62191 | Date Created: 06/11/2009 | Last Updated: 07/01/2016
Access Level: TechConnect

Question
How can I use ActiveX Data Objects (ADO) to connect to a SQL 2005 Express Database?

Answer
The following code will connect to a SQL 2005 Express database that is running locally (on the same PC that the code is executed from).

It was executed on a FactoryTalk View SE CPR9 Display.


SQL 2005 Express was installed following the instructions in Appendix D of the FactoryTalk Alarm and Events Quick Start Guide
(http://literature.rockwellautomation.com/idc/groups/literature/documents/qs/FactoryTalk Activationse-qs001_-en-e.pdf), pub. no. FactoryTalk ActivationsE-QS001A-EN-
E, August 2007.
You need to add the Microsoft ActiveX Data Objects 2.8 Library.

This example will connect to a database named FTAlarms, which was being used to log Alarm and Event history. Use your own database name in the code below.

When the button is pressed, it does the following:

Connects to the database.


Queries the FTser.TrackingEvent table.
Gets the information from the SourceName and Message columns.
Populates the listbox.

To connect to a SQL 2005 Express database running locally, complete the following steps:

1. Create a new display.


a. Add a Microsoft Forms 2.0 Listbox control and a Command Button.
b. The listbox control should have a default name of ListBox1, which can be checked by right-clicking on it, then selecting Property Panel.
c. Make the control about half the size of the display.
2. Right click on the Command Button and select Property Panel.

Make sure ExposeToVBA is set to VBA control.

Do the same for the Listbox.

3. Right-click on the Command Button and select VBA code.

4. Copy and paste the code below into the sub routine.

Make sure you change the Database to the one you want to connect to and the correct table when doing the query.

5. Save it, put the display in Test Run and press the button. The Listbox should be populated.

Below is the code to use in step 4.

Dim Con As New ADODB.Connection


Dim rst As New ADODB.Recordset

Con.ConnectionString = "Provider=SQL Native Client;" _


& "Server=.\SQLEXPRESS;" _
& "Database=FTAlarms;" _
& "Trusted_Connection=YES;" _
& "DataTypeCompatibility=80;" _
& "MARS Connection=True;"
Con.Open

Set rst.ActiveConnection = Con


rst.ActiveConnection = Con
rst.Source = "Select * From FTser.TrackingEvent"
rst.Open
ListBox1.Visible = False
ListBox1.AddItem "Alarm Name"
While Not rst.EOF
ListBox1.AddItem rst.Fields("SourceName").Value & " --------- " & rst.Fields("Message").Value

https://rockwellautomation.custhelp.com/app/answers/detail/a_id/62191/related/1 1/2
10/10/2017 62191 - Connect to SQL 2005 Express Database Using ADO
rst.MoveNext
Wend

ListBox1.Visible = True

rst.Close
Set rst = Nothing
Con.Close
Set Con = Nothing

DISCLAIMER
This knowledge base web site is intended to provide general technical information on a particular subject or subjects and is not an exhaustive treatment of such subjects.
Accordingly, the information in this web site is not intended to constitute application, design, software or other professional engineering advice or services. Before making any
decision or taking any action, which might affect your equipment, you should consult a qualified professional advisor.

ROCKWELL AUTOMATION DOES NOT WARRANT THE COMPLETENESS, TIMELINESS OR ACCURACY OF ANY OF THE DATA CONTAINED IN THIS WEB SITE AND
MAY MAKE CHANGES THERETO AT ANY TIME IN ITS SOLE DISCRETION WITHOUT NOTICE. FURTHER, ALL INFORMATION CONVEYED HEREBY IS PROVIDED TO
USERS "AS IS." IN NO EVENT SHALL ROCKWELL BE LIABLE FOR ANY DAMAGES OF ANY KIND INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL,
LOSS PROFIT OR DAMAGE, EVEN IF ROCKWELL AUTOMATION HAVE BEEN ADVISED ON THE POSSIBILITY OF SUCH DAMAGES.

ROCKWELL AUTOMATION DISCLAIMS ALL WARRANTIES WHETHER EXPRESSED OR IMPLIED IN RESPECT OF THE INFORMATION (INCLUDING SOFTWARE)
PROVIDED HEREBY, INCLUDING THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, AND NON-INFRINGEMENT. Note
that certain jurisdictions do not countenance the exclusion of implied warranties; thus, this disclaimer may not apply to you.

This page is best viewed in Internet Explorer 11, Edge, Firefox 41, Chrome 46, Safari 9 or newer.

https://rockwellautomation.custhelp.com/app/answers/detail/a_id/62191/related/1 2/2