Page 1 of 2 12 LastLast
Results 1 to 40 of 48

Thread: Pick a file to import?[RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    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:
    1. Dim excel As String = "exec master.dbo.xp_cmdshell 'dtsrun -E -S crserver -N High_Accurint'"
    2.             Dim SqlConn56 As SqlConnection = New SqlConnection(strConnectionString)
    3.             Dim cmdSqlCommand56 As SqlCommand = New SqlCommand(excel, SqlConn56)
    4.             Try
    5.                 cmdSqlCommand56.Connection.Open()
    6.                 cmdSqlCommand56.ExecuteNonQuery()
    7.             Catch ex As SqlException
    8.                 MessageBox.Show(ex.Message)
    9.                 Exit Sub
    10.             Finally
    11.                 If SqlConn56.State = ConnectionState.Open Then
    12.                     SqlConn56.Close()
    13.                 End If
    14.             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?

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    I dont have the code with me but try at SQLDTS.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  4. #4
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    But where do I put that coding? This is what I have right now:
    VB Code:
    1. Dim excel As String = "exec master.dbo.xp_cmdshell 'dtsrun -E -S crserver -N High_Accurint'"
    2.         Dim SqlConn56 As SqlConnection = New SqlConnection(strConnectionString)
    3.         Dim cmdSqlCommand56 As SqlCommand = New SqlCommand(excel, SqlConn56)
    4.         Try
    5.             cmdSqlCommand56.Connection.Open()
    6.             cmdSqlCommand56.ExecuteNonQuery()
    7.         Catch ex As SqlException
    8.             MessageBox.Show(ex.Message)
    9.             Exit Sub
    10.         Finally
    11.             If SqlConn56.State = ConnectionState.Open Then
    12.                 SqlConn56.Close()
    13.             End If
    14.         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?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    How does the package get changed? Anyone have any ideas?

    Thanks!
    Brenda

    If it weren't for you guys, where would I be?

  7. #7
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    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.

  8. #8
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  10. #10
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    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.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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:
    1. Dim cnn As DTS.Connection
    2.         Dim pkg As New DTS.Package()
    3.         Dim objstep As DTS.Step
    4.  
    5.         pkg.LoadFromSQLServer("CRSERVER", "Administrator", "*******", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, "Accurint_High")
    6.         Dim ofd As New OpenFileDialog()
    7.         ofd.ShowDialog()
    8.         cnn = [color=red]pkg.Connections[/color](ofd.FileName)
    9.         cnn.DataSource = "data source=CRSERVER;initial catalog=crdatabase;integrated security=SSPI;persist security info=False;workstation id=CRSERVER;packet size=4096;"
    10.  
    11.         cnn = Nothing
    12.  
    13.         For Each objstep In pkg.Steps
    14.  
    15.             objstep.ExecuteInMainThread = True
    16.  
    17.         Next objstep
    18.  
    19.         pkg.Execute()
    20.         pkg.UnInitialize()

    What is wrong? Any ideas?
    Brenda

    If it weren't for you guys, where would I be?

  12. #12
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    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.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Can anyone see why the line in red won't work?
    VB Code:
    1. Dim cnn As DTS.Connection
    2.         Dim pkg As New DTS.Package()
    3.         Dim objstep As DTS.Step
    4.  
    5.         pkg.LoadFromSQLServer("CRSERVER", "brenda", "*********", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, , , , "Accurint_High")
    6.         Dim ofd As New OpenFileDialog()
    7.         ofd.ShowDialog()
    8.         [color=red]cnn = pkg.Connections("Connection 1")[/color]
    9.         cnn.DataSource = ofd.FileName
    10.         cnn = Nothing
    11.  
    12.         For Each objstep In pkg.Steps
    13.  
    14.             objstep.ExecuteInMainThread = True
    15.  
    16.         Next objstep
    17.  
    18.         pkg.Execute()
    19.         pkg.UnInitialize()
    Thanks for any help
    Brenda

    If it weren't for you guys, where would I be?

  14. #14
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    It needs to be SET cnn = blah blah blah

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  16. #16
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Sorry Brenda I dont know why it would do that

    The code I posted above was my working code.....

  17. #17
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Have you added all the references for SQL DTS to your project? That may be the problem (grasping at straws a bit here)

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  20. #20
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    I added the following refs when I did this:

    Microsoft DTSDataPump Scripting Object library
    Microsoft DTSPackage Object library
    Microsoft DTS Custom Tasks Object library

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    I think I am gonna go crazy. Does anyone have any idea why this won't work:
    VB Code:
    1. 'change DTS Package
    2.         Dim oPKG As DTS.Package, oStep As DTS.Step
    3.         oPKG = New DTS.Package
    4.         Dim oConn As DTS.Connection
    5.  
    6.         Dim sServer As String, sUsername As String, sPassword As String
    7.         Dim sPackageName As String, sMessage As String
    8.         Dim lErr As Long, sSource As String, sDesc As String
    9.  
    10.         ' Set Parameter Values
    11.         sServer = "CRSERVER"
    12.         sUsername = "brenda"
    13.         sPassword = "g_Password"
    14.         sPackageName = "Accurint_High"
    15.  
    16.         ' Set Filename
    17. [color=red]        oConn = oPKG.Connections("Connection 1")[/color]
    18.         Dim ofd As New OpenFileDialog
    19.         ofd.ShowDialog()
    20.         oConn.DataSource = ofd.FileName
    21.         oConn = Nothing
    22.  
    23.         ' Load Package
    24.         oPKG.LoadFromSQLServer(sServer, sUsername, sPassword, _
    25.             DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, , , , sPackageName)
    26.  
    27.         ' Set Exec on Main Thread
    28.         For Each oStep In oPKG.Steps
    29.             oStep.ExecuteInMainThread = True
    30.         Next
    31.  
    32.         ' Execute
    33.         oPKG.Execute()
    34.  
    35.         ' Get Status and Error Message
    36.         For Each oStep In oPKG.Steps
    37.             If oStep.ExecutionResult = DTS.DTSStepExecResult.DTSStepExecResult_Failure Then
    38.                 oStep.GetExecutionErrorInfo(lErr, sSource, sDesc)
    39.                 sMessage = sMessage & "Step """ & oStep.Name & _
    40.                     """ Failed" & vbCrLf & _
    41.                     vbTab & "Error: " & lErr & vbCrLf & _
    42.                     vbTab & "Source: " & sSource & vbCrLf & _
    43.                     vbTab & "Description: " & sDesc & vbCrLf & vbCrLf
    44.             Else
    45.                 sMessage = sMessage & "Step """ & oStep.Name & _
    46.                     """ Succeeded" & vbCrLf & vbCrLf
    47.             End If
    48.         Next
    49.  
    50.         oPKG.UnInitialize()
    51.  
    52.         oStep = Nothing
    53.         oPKG = Nothing
    54.  
    55.         ' Display Results
    56.         MsgBox(sMessage)

    It doesn't like the line in red Any ideas?
    Brenda

    If it weren't for you guys, where would I be?

  22. #22
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    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

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  24. #24
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Without testing it, it seems like it should be:
    VB Code:
    1. ' Load Package
    2.         oPKG.LoadFromSQLServer(sServer, sUsername, sPassword, _
    3.             DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, , , , sPackageName)
    4.  
    5.        ' Set Filename
    6.         oConn = oPKG.Connections("Connection 1")
    7.         Dim ofd As New OpenFileDialog
    8.         ofd.ShowDialog()
    9.         oConn.DataSource = ofd.FileName
    10.         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

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  26. #26
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Is it still failing at the oConn = oPkg.Connections("Connection 1") line?
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  28. #28
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Try change this line:
    VB Code:
    1. oConn = oPKG.Connections("Connection 1")
    to:
    VB Code:
    1. oConn = oPKG.Connections.Items("Connection 1")
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  30. #30
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You would think so....


    Try changing the following block of code:
    VB Code:
    1. oConn = oPKG.Connections("Connection 1")
    2.         Dim ofd As New OpenFileDialog
    3.         ofd.ShowDialog()
    4.         oConn.DataSource = ofd.FileName
    5.         oConn = Nothing
    to:
    VB Code:
    1. Dim ofd As New OpenFileDialog
    2.         ofd.ShowDialog()
    3.         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

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    What would this do:

    oPKG.GlobalVariables("MyGlobalVariable").Value = txtGlobalVariable.Text
    Brenda

    If it weren't for you guys, where would I be?

  33. #33
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    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

  34. #34

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  35. #35
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    I'm out of answers (or guesses, which ever you prefer).
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  36. #36

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  37. #37
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    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.

  38. #38

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  39. #39

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    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?

  40. #40
    Lively Member
    Join Date
    Sep 2004
    Posts
    96
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width