I have a DTS package written to a visual basic module. Now I am trying to make some changes to it, and compile it into an EXE file. What references do I need in my VB project to work with the code that SQL created for this DTS package?
I have never written a package that way before, but I do know to execute a package saved on your SQL Server (which is where I imagine you plan to put it?) from VB you need a reference to the 'Microsoft DTSPackage Object Library'. Or you can use the command line utility DTSRun, but I don't like that method.
Hope this helps,
Alex
Ignorance of certain subjects is a great part of wisdom
adding a reference to Microsoft DTSPackage Object Library worked - thank you Alex
Incidentally here's what I'm doing: I created a DTS package when I was using the data import wizard in SQL Enterprise Manager. I then was executing the DTS package from there for each separate text file that I was importing, but before I ran the package, I was renaming the text file so that it matched the name of the source file referred to in the DTS package.
I decided to try and save a new DTS package only this time right it to a VB module, what I want to try and do is add some lines to the code for this DTS package, that will prompt me with an inputbox to enter a file name, and then have that file name be my Data Source. Then compile this module into an EXE file, and run my DTS package from that?
Hopefully that will work. Also I would like to set it up so that when it imports the data, for each record inserted, it records the name of the text file that referred to above, in one of the columns in the table. Is this possible?
You can do that, you just need to assign the source file name using vbScript (or you can pass a global variable from your front end app). I may be able to track down an example of how to change it if you need it.
Good Luck
Alex
Ignorance of certain subjects is a great part of wisdom
Here's the code that SQL generated in VB when I created the DTS package:
Option Explicit
Public goPackageOld As New DTS.Package
Public goPackage As DTS.Package2
Public MyTxtFileName As String
Private Sub Main()
Set goPackage = goPackageOld
'---------------------------------------------------------------------------
' create package connection information
'---------------------------------------------------------------------------
Dim oConnection As DTS.Connection2
'------------- a new connection defined below.
'For security purposes, the password is never scripted
MyTxtFileName = InputBox("Enter file name, without the .csv extension", "File Name")
Set oConnection = goPackage.Connections.New("DTSFlatFile")
'If you have a password for this connection, please uncomment and add your password below.
'oConnection.Password = "<put the password here>"
goPackage.Connections.Add oConnection
Set oConnection = Nothing
'---------------------------------------------------------------------------
' create package steps information
'---------------------------------------------------------------------------
Dim oStep As DTS.Step2
Dim oPrecConstraint As DTS.PrecedenceConstraint
'------------- a new step defined below
Set oStep = goPackage.Steps.New
oStep.Name = "Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Step"
oStep.Description = "Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Step"
oStep.ExecutionStatus = 1
oStep.TaskName = "Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Task"
oStep.CommitSuccess = False
oStep.RollbackFailure = False
oStep.ScriptLanguage = "VBScript"
oStep.AddGlobalVariables = True
oStep.RelativePriority = 3
oStep.CloseConnection = False
oStep.ExecuteInMainThread = False
oStep.IsPackageDSORowset = False
oStep.JoinTransactionIfPresent = False
oStep.DisableStep = False
oStep.FailPackageOnError = False
goPackage.Steps.Add oStep
Set oStep = Nothing
'---------------------------------------------------------------------------
' create package tasks information
'---------------------------------------------------------------------------
'------------- call Task_Sub1 for task Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Task (Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Task)
Call Task_Sub1(goPackage)
'---------------------------------------------------------------------------
' Save or execute package
'---------------------------------------------------------------------------
'goPackage.SaveToSQLServer "(local)", "sa", ""
goPackage.Execute
goPackage.UnInitialize
'to save a package instead of executing it, comment out the executing package line above and uncomment the saving package line
Set goPackage = Nothing
Set goPackageOld = Nothing
End Sub
'------------- define Task_Sub1 for task Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Task (Copy Data from 01000001[1] to [World_Alliance].[dbo].[LD_Usage] Task)
Public Sub Task_Sub1(ByVal goPackage As Object)
Dim oTask As DTS.Task
Dim oLookup As DTS.Lookup
Dim oCustomTask1 As DTS.DataPumpTask2
Set oTask = goPackage.Tasks.New("DTSDataPumpTask")
Set oCustomTask1 = oTask.CustomTask
goPackage.Tasks.Add oTask
Set oCustomTask1 = Nothing
Set oTask = Nothing
End Sub
Public Sub oCustomTask1_Trans_Sub1(ByVal oCustomTask1 As Object)
Dim oTransformation As DTS.Transformation2
Dim oTransProps As DTS.Properties
Dim oColumn As DTS.Column
Set oTransformation = oCustomTask1.Transformations.New("DTS.DataPumpTransformCopy")
oTransformation.Name = "DirectCopyXform"
oTransformation.TransformFlags = 63
oTransformation.ForceSourceBlobsBuffered = 0
oTransformation.ForceBlobsInMemory = False
oTransformation.InMemoryBlobSize = 1048576
oTransformation.TransformPhases = 4
oTransformation.DestinationColumns.Add oColumn
Set oColumn = Nothing
Set oTransProps = oTransformation.TransformServerProperties
Set oTransProps = Nothing
oCustomTask1.Transformations.Add oTransformation
Set oTransformation = Nothing
End Sub
As you can see I declared a public string variable called MyTxtFileName, and I added an InputBox to prompt the user to set the value of this variable to the name of the text file they want to import. They there are three places in the code that refer to this file as the 'data source', and I changed it so it used the value of this variable as the name of the file.
This is all I have so far. I am currently working on this in Visual Basic 6.0.
Would there be an easier way to do this? For example is there a way to do this from within SQL, perhaps through Enterprise Manager, where you can have a DTS package already set up, but have it prompt you for the name of the source file when you execute it?
That way I could skip this process in VB, then the only other thing would be getting it to populate one of the columns in the table with the name of the imported text file.
I apologize but apparently it will not let me post the entire code - is there a way to attach a file to a post, or is there a way to format that code differently so it all fits in the post?
I don't have time to read all of that, but if you are going to be setting a whole ton of different text file connections, I think you should read up on the dynamic properties task.
Have a look here, the gents at sqldts.com will walk you through creating a package to import every text file in a specified directory. You can modify what they are doing there to suit your needs.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.