|
-
Oct 8th, 2004, 03:57 PM
#1
Thread Starter
Hyperactive Member
Pick a file to import?[RESOLVED]
Does anyone know if there is a way to be able to select a file you want to import if you already have a DTS setup? This is what I have right now:
VB Code:
Dim excel As String = "exec master.dbo.xp_cmdshell 'dtsrun -E -S crserver -N High_Accurint'"
Dim SqlConn56 As SqlConnection = New SqlConnection(strConnectionString)
Dim cmdSqlCommand56 As SqlCommand = New SqlCommand(excel, SqlConn56)
Try
cmdSqlCommand56.Connection.Open()
cmdSqlCommand56.ExecuteNonQuery()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Exit Sub
Finally
If SqlConn56.State = ConnectionState.Open Then
SqlConn56.Close()
End If
End Try
If there is no way to do this, I can just manually change the name of the file everytime, but who wants to do that.
Any ideas?
Last edited by brendalisalowe; Dec 10th, 2004 at 01:21 PM.
Brenda
If it weren't for you guys, where would I be?
-
Oct 9th, 2004, 03:06 PM
#2
Hyperactive Member
I dont have the code with me but try at SQLDTS.com
-
Oct 11th, 2004, 10:40 AM
#3
Thread Starter
Hyperactive Member
I went to this website (http://www.sqldts.com/default.aspx?208,1), but am having trouble figuring out how to do it, as usual. Can anyone help me out? Thanks!
Brenda
If it weren't for you guys, where would I be?
-
Oct 11th, 2004, 11:05 AM
#4
Hyperactive Member
Whats the prob? the bit you want is on page 3
' Set Filename
Set oConn = oPKG.Connections("TextFile")
oConn.DataSource = txtFilename.Text
Set oConn = Nothing
-
Oct 11th, 2004, 03:42 PM
#5
Thread Starter
Hyperactive Member
But where do I put that coding? This is what I have right now:
VB Code:
Dim excel As String = "exec master.dbo.xp_cmdshell 'dtsrun -E -S crserver -N High_Accurint'"
Dim SqlConn56 As SqlConnection = New SqlConnection(strConnectionString)
Dim cmdSqlCommand56 As SqlCommand = New SqlCommand(excel, SqlConn56)
Try
cmdSqlCommand56.Connection.Open()
cmdSqlCommand56.ExecuteNonQuery()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Exit Sub
Finally
If SqlConn56.State = ConnectionState.Open Then
SqlConn56.Close()
End If
End Try
My file is called High_Accurint, but every month it changes. How do I make so I can pick the file? Thanks for your help!
Brenda
If it weren't for you guys, where would I be?
-
Oct 12th, 2004, 02:51 PM
#6
Thread Starter
Hyperactive Member
How does the package get changed? Anyone have any ideas?
Thanks!
Brenda
If it weren't for you guys, where would I be?
-
Oct 12th, 2004, 05:55 PM
#7
Hyperactive Member
I have code at work, I will post an example for you tomorrow.
Basically, just connect to your SQL Server, set a reference to the package then change the target of the connection in question and execute it.
-
Oct 13th, 2004, 03:56 AM
#8
Hyperactive Member
Code:
Dim cnn As DTS.Connection
Dim pkg As New DTS.Package
Dim objstep As DTS.step
pkg.LoadFromSQLServer "ServerName", "UserName", "Password", DTSSQLStgFlag_Default, , , , "PackageName"
Set cnn = pkg.Connections("TextFileImport")
cnn.DataSource = "MyFilePath"
Set cnn = Nothing
For Each objstep In pkg.Steps
objstep.ExecuteInMainThread = True
Next objstep
pkg.Execute
pkg.UnInitialize
You need to replace everything in quotes with the names of your parameters
Hope that helps
-
Oct 13th, 2004, 09:31 AM
#9
Thread Starter
Hyperactive Member
It doesn't like this:
DTSSQLStgFlag_Default, , , ,
It puts a blue squiggley line under it.
Also, it doesn't like this:
pkg.Connections("TextFileImport")
Any idea why?
What do I put in here?: ("TextFileImport")
Do I use OpenFileDialog?
And this: "MyFilePath"
Thanks for your help!
Brenda
If it weren't for you guys, where would I be?
-
Oct 13th, 2004, 09:41 AM
#10
Hyperactive Member
You need to replace "TextFileImport" with the name of the connection between the database and the file you are importing from your DTS package. When you create a DTS package from the wizard, this is usually called "Connection 1"
MyFilePath is just the path of the file you wish to import. Best to use a dialogue or whatever in another sub and pass the selected filepath into this sub.
The pkg.LoadFromSQLServer method requires several parameters. One is that DTSSQLStgFlag_Default constant and some others are optional, hence the , , , ,
To be honest I really dont know the ins and outs of this very well I got all my info from the SQLDTS.com site.
-
Oct 13th, 2004, 09:53 AM
#11
Thread Starter
Hyperactive Member
For this: pkg.Connections
It says" Interface 'DTS.Connections' cannot be indexed because it has no default property."
Do you know what that means?
This is what I have so far:
VB Code:
Dim cnn As DTS.Connection
Dim pkg As New DTS.Package()
Dim objstep As DTS.Step
pkg.LoadFromSQLServer("CRSERVER", "Administrator", "*******", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, "Accurint_High")
Dim ofd As New OpenFileDialog()
ofd.ShowDialog()
cnn = [color=red]pkg.Connections[/color](ofd.FileName)
cnn.DataSource = "data source=CRSERVER;initial catalog=crdatabase;integrated security=SSPI;persist security info=False;workstation id=CRSERVER;packet size=4096;"
cnn = Nothing
For Each objstep In pkg.Steps
objstep.ExecuteInMainThread = True
Next objstep
pkg.Execute()
pkg.UnInitialize()
What is wrong? Any ideas?
Brenda
If it weren't for you guys, where would I be?
-
Oct 13th, 2004, 10:02 AM
#12
Hyperactive Member
Hmmm ok, how to explain it......
If you import a text file with the DTS wizard and then save the package, then look at the diagram of your new package you have a picture of a file and a picture of a sort-of computery thing with an arrow between them. If you look at the properties of the picture of the file, its name is "Connection 1". You can rename it.
That is the name you want to put in:
cnn = pkg.Connections("Connection 1")
Sorry I cant be more helpful, i work elsewhere now and we dont use SQL Server so im doing this from my (famously poor) memory.
-
Oct 13th, 2004, 10:52 AM
#13
Thread Starter
Hyperactive Member
Can anyone see why the line in red won't work?
VB Code:
Dim cnn As DTS.Connection
Dim pkg As New DTS.Package()
Dim objstep As DTS.Step
pkg.LoadFromSQLServer("CRSERVER", "brenda", "*********", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, , , , "Accurint_High")
Dim ofd As New OpenFileDialog()
ofd.ShowDialog()
[color=red]cnn = pkg.Connections("Connection 1")[/color]
cnn.DataSource = ofd.FileName
cnn = Nothing
For Each objstep In pkg.Steps
objstep.ExecuteInMainThread = True
Next objstep
pkg.Execute()
pkg.UnInitialize()
Thanks for any help
Brenda
If it weren't for you guys, where would I be?
-
Oct 13th, 2004, 10:57 AM
#14
Hyperactive Member
It needs to be SET cnn = blah blah blah
-
Oct 13th, 2004, 11:09 AM
#15
Thread Starter
Hyperactive Member
Everytime I type in the word SET, it takes it away. Like it doesn't like it. Do I need to put quotes or anything?
Thanks for all your time Granty!
Brenda
If it weren't for you guys, where would I be?
-
Oct 13th, 2004, 11:23 AM
#16
Hyperactive Member
Sorry Brenda I dont know why it would do that
The code I posted above was my working code.....
-
Oct 13th, 2004, 11:41 AM
#17
Hyperactive Member
Have you added all the references for SQL DTS to your project? That may be the problem (grasping at straws a bit here)
-
Oct 14th, 2004, 03:08 PM
#18
Thread Starter
Hyperactive Member
What references need to be added? I have the DTS reference in it already.
Brenda
If it weren't for you guys, where would I be?
-
Oct 14th, 2004, 05:09 PM
#19
Thread Starter
Hyperactive Member
Has anyone ever heard of sp_update_job? Is that what I am supposed to use?
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 02:46 AM
#20
Hyperactive Member
I added the following refs when I did this:
Microsoft DTSDataPump Scripting Object library
Microsoft DTSPackage Object library
Microsoft DTS Custom Tasks Object library
-
Oct 15th, 2004, 09:45 AM
#21
Thread Starter
Hyperactive Member
I think I am gonna go crazy. Does anyone have any idea why this won't work:
VB Code:
'change DTS Package
Dim oPKG As DTS.Package, oStep As DTS.Step
oPKG = New DTS.Package
Dim oConn As DTS.Connection
Dim sServer As String, sUsername As String, sPassword As String
Dim sPackageName As String, sMessage As String
Dim lErr As Long, sSource As String, sDesc As String
' Set Parameter Values
sServer = "CRSERVER"
sUsername = "brenda"
sPassword = "g_Password"
sPackageName = "Accurint_High"
' Set Filename
[color=red] oConn = oPKG.Connections("Connection 1")[/color]
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
oConn.DataSource = ofd.FileName
oConn = Nothing
' Load Package
oPKG.LoadFromSQLServer(sServer, sUsername, sPassword, _
DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, , , , sPackageName)
' Set Exec on Main Thread
For Each oStep In oPKG.Steps
oStep.ExecuteInMainThread = True
Next
' Execute
oPKG.Execute()
' Get Status and Error Message
For Each oStep In oPKG.Steps
If oStep.ExecutionResult = DTS.DTSStepExecResult.DTSStepExecResult_Failure Then
oStep.GetExecutionErrorInfo(lErr, sSource, sDesc)
sMessage = sMessage & "Step """ & oStep.Name & _
""" Failed" & vbCrLf & _
vbTab & "Error: " & lErr & vbCrLf & _
vbTab & "Source: " & sSource & vbCrLf & _
vbTab & "Description: " & sDesc & vbCrLf & vbCrLf
Else
sMessage = sMessage & "Step """ & oStep.Name & _
""" Succeeded" & vbCrLf & vbCrLf
End If
Next
oPKG.UnInitialize()
oStep = Nothing
oPKG = Nothing
' Display Results
MsgBox(sMessage)
It doesn't like the line in red Any ideas?
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 09:59 AM
#22
Fanatic Member
Wouldn't you need to Load the package first, then set it's connections?
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 10:02 AM
#23
Thread Starter
Hyperactive Member
Like this:
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
oConn.DataSource = ofd.FileName
oConn = oPKG.Connections("Connection 1")
oConn = Nothing
It doesn't like that either. Any other ideas?
Look at this website:
http://www.sqldts.com/default.aspx?226
I think I need something like this, but can't figure out how to do it.
Last edited by brendalisalowe; Oct 15th, 2004 at 10:06 AM.
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 10:13 AM
#24
Fanatic Member
Without testing it, it seems like it should be:
VB Code:
' Load Package
oPKG.LoadFromSQLServer(sServer, sUsername, sPassword, _
DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, , , , sPackageName)
' Set Filename
oConn = oPKG.Connections("Connection 1")
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
oConn.DataSource = ofd.FileName
oConn = Nothing
Load the package first, then try to set the connection source property
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 10:18 AM
#25
Thread Starter
Hyperactive Member
Good thinking, but it still doesn't like it. Want me to send you my code? I am using VS.NET 2003.
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 10:21 AM
#26
Fanatic Member
Is it still failing at the oConn = oPkg.Connections("Connection 1") line?
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 10:23 AM
#27
Thread Starter
Hyperactive Member
It doesn't let me run it because there is a blue squiggley line under it. When I put my mouse over that line it says:
"Interface 'DTS.Connections' cannot be indexed because it has no default property."
Ever seen that before?
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 10:44 AM
#28
Fanatic Member
Try change this line:
VB Code:
oConn = oPKG.Connections("Connection 1")
to:
VB Code:
oConn = oPKG.Connections.Items("Connection 1")
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 11:18 AM
#29
Thread Starter
Hyperactive Member
Well, it is kind of working now. It runs through the code and even lets me pick the file to import, but it always just imports the file that is saved in the DTS Package. Shouldn't it be changing the Excel file that I pick? Any ideas?
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 11:25 AM
#30
Fanatic Member
You would think so....
Try changing the following block of code:
VB Code:
oConn = oPKG.Connections("Connection 1")
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
oConn.DataSource = ofd.FileName
oConn = Nothing
to:
VB Code:
Dim ofd As New OpenFileDialog
ofd.ShowDialog()
oPKG.Connections("Connection 1").DataSource = ofd.FileName
What is OpenFileDialog? Is it that DLL from the SQLDTS website?
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 11:49 AM
#31
Thread Starter
Hyperactive Member
It still isn't working. OpenFileDialog is a window where you pick the file you want. Have you not seen that before? Is that the best way to do it, or is there a better way?
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 12:07 PM
#32
Thread Starter
Hyperactive Member
What would this do:
oPKG.GlobalVariables("MyGlobalVariable").Value = txtGlobalVariable.Text
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 12:25 PM
#33
Fanatic Member
That just sets a Global variable in the DTS package.
Have you tried hardcoding the path/filename into the application instead of using the OpenFileDialog?
Something like
oPkg.Connections("Connection 1").DataSource = "C:\...."
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 12:35 PM
#34
Thread Starter
Hyperactive Member
I tried this:
oPKG.Connections.Item("Connection 1").DataSource = "C:\Accurint\Received\High_804.xls"
It still reads the old file that is stored in the package. Weird huh? Does ANYONE know how to change that?
Brenda
If it weren't for you guys, where would I be?
-
Oct 15th, 2004, 12:44 PM
#35
Fanatic Member
I'm out of answers (or guesses, which ever you prefer).
Chris
Master Of My Domain
Got A Question? Look Here First
-
Oct 15th, 2004, 03:52 PM
#36
Thread Starter
Hyperactive Member
So I've been reading some information on this crazy stuff. Do you know anything about ActiveX Script? I am thinking I need to setup that up for global variables, but I can't figure out how. Has anyone ever used that before?
Brenda
If it weren't for you guys, where would I be?
-
Oct 18th, 2004, 03:34 PM
#37
Hyperactive Member
Its very strange how it removes that SET because Im sure that's right......try turning code validation off and using SET?
You can do a ton with ActiveX scripts etc, DTS is pretty versatile. A colleague I used to work with did some really cool stuff but I just touched the bare bones myself Im afraid.
-
Oct 19th, 2004, 05:31 PM
#38
Thread Starter
Hyperactive Member
How do you turn off code validation? I'm still going crazy with this
Brenda
If it weren't for you guys, where would I be?
-
Nov 4th, 2004, 01:11 PM
#39
Thread Starter
Hyperactive Member
I've still NEVER figured this out I don't know if I have really explained well of what I need to do. I need to be able to choose the Excel file that I want imported into SQL Server. Do I need to use Active Scripting when I create the package? How? I really would like to get this figured out. Any help would be GREAT! Thanks!
Brenda
If it weren't for you guys, where would I be?
-
Nov 4th, 2004, 02:40 PM
#40
Lively Member
Thinking outsidethe box for a moment.......
Does the DTS need to be saved on the server?
Here's a thought. Open the DTS package, then File - Save As the package and save it as a VB Module. Create a VB project and add the module to it. You'll also need to set some of the DTS object references as well.
In the code, you could borwse and set the file location/name there and then run it, or you can save it. If you want to be able to pick the file and have it run immedialty, that would be ideal. If it's something that's scheduled to run later, then savign it back to the server will give it the new filename.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|