SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Executing a Package from Visual Basic

By Brian Knight, 2004/01/23

Total article views: 24062 | Views in the last 30 days: 85

So you've created a SQL Server package and now you're ready to integrate it into your Visual Basic application? A common scenario for this is where you don't want a client to have direct access to SQL Server Enterprise Manager. Instead, you can send them a Visual Basic executable to place a clean front-end to the package. For example, you could send the client a program to where you could enter a few parameters to execute the package. In this article, I'll show you how to do a simple example.

The first step is to create a DTS package with one global variable. To create a global variable select Properties from the Package menu then go to the Global Variables tab. For this example, call the global variable gvHello and type the value of "Hi World" (without quotes) as shown below.

Next, create an ActiveX Script Task under the Tasks menu. The only line of code you'll have to add is:

Msgbox DTSGlobalVariables("gvHello").Value

Here's the full task:

Now that you have the package created, save it as SamplePackage locally on the server. Upon execution, you should see the following results from a popup.

Now that your package is created, we're ready to call it from Visual Basic. Open Visual Basic and create a new Standard EXE program. Under the Projects menu, select References. About half-way down, check the Microsoft DTSPackage Object Library item as shown below. This will inform Visual Basic of the DTS object model and enable itellisense to easily find objects for you. It is not required, but it will make your life easier.

Next create a simple Visual Basic app with

  • One text box called txtGlobalVariable
  • One command button called cmdExecute

It should look something like this:

Double-click on the Execute Package command button to set the command properties. The code is going to be someone simple to perform our basic task. We will want the user to be able to enter a value in the text box and the click on the Execute Package button. When the Execute Package button is set, the global variable will be set to the text box's value and then the package will be executed. To reference the DTS object model, you'll need to declare the object as shown below:

Dim objPackage As New Package

Next, you must load the package. Since we saved the package on the SQL Server, then we will use the LoadFromSQLServer method. If you were to save it as a structured file, then you'd use the LoadFromStructuredFile method. The following lines are an example on how to load the package we saved earlier. My server name is called "ServerName". Notice that I'm using a trusted connection versus a SQL login.

objPackage.LoadFromSQLServer "ServerName", , , _
DTSSQLStgFlag_UseTrustedConnection, , , , "SamplePackage"

To set the global variable, you must use the GlobalVariables collection as shown below:

objPackage.GlobalVariables.Item("gvHello") = txtGlobalVariable.Text

Finally, we execute the package and then unload it as shown here:

objPackage.Execute
objPackage.UnInitialize
 

Here's the complete example: (you can also download it here).

Private Sub cmdExecute_Click()
Dim objPackage As New Package

objPackage.LoadFromSQLServer "Xanadu", , , _
DTSSQLStgFlag_UseTrustedConnection, , , , "SamplePackage"
 

objPackage.GlobalVariables.Item("gvHello") = txtGlobalVariable.Text

objPackage.Execute
objPackage.UnInitialize

End Sub

Go ahead, try to execute your program now and enter a sample value:

In the next article, I'll show you a little bit more of the DTS object model and how to add some more sophisticated error handling into your program.

By Brian Knight, 2004/01/23

Total article views: 24062 | Views in the last 30 days: 85
Your response
 
 
Related tags

DTS     SQL Server 7, 2000    
Programming     Visual Basic 6    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com