Results 1 to 4 of 4

Thread: Text file to SQL server hellp puleaase!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    11

    Red face Text file to SQL server hellp puleaase!

    Dear VB Elite,

    I have this CSV file saved as juvrec.txt from a database from this old DOS program DataEase...[hell]... I need to figure out a way to get a VB program to read the CSV and export it to a database in SQL Server. Any help/starter suggestions would be excellent because I am clueless as where to begin.

    EX:
    The first line of the text file is column names for the table. The second and so on are the actual fields.

    The second line is "00001","ABBOTT","GEORGE","RYAN","",,W,M,9/9/1979,"","MI","1290 PARK RD"

    How would I get the program to take the strings and put it in a desired column in a table in SQL? I hope this post makes sense because I'm clueless and lost. Thank you for even reading this.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    India
    Posts
    342

    try like this...

    open ur text file for Input ...using instr function seperate the fields(since each field is sepetaed with a 'Comma'..).. insert the data u got from the above method into ur required table using DAO/ ADOs...
    ksm

  3. #3
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You could use DTS to handle this. It allows you to import a text file into a table. This would be the quickest way.

    But...

    If you need to do it through VB, read in each line of the file, use the Replace function to remove the double quotes, use the split function to populate an array, then execute an Insert statement to upload the data to your table.
    VB Code:
    1. Dim strLine As String
    2. Dim strNew As String
    3. Dim arrFile()
    4. Dim strSQL As String
    5.  
    6. Open "C:\FileName.csv" For Input As #1
    7. 'Read the first line to get rid of the column headers
    8. Line Input #1, strLine
    9.  
    10. Do Until EOF(1)
    11.      'Read the next line in the file
    12.      Line Input #1, strLine
    13.      'Replace the double quotes with nothing
    14.      strNew = Replace(strLine, """, "")
    15.      'Put the string in an array
    16.      arrFile = Split(strNew, ",")
    17.      'Build the Insert statement
    18.      strSQL = "Insert Into myTable(Field1, Field2, Field3) Values ("
    19.      strSQL = strSQL & "'" & arrFile(0) & "', " & arrFile(1) & ")"
    20.      'Execute the insert statement with the Connection Object
    21.      cn.Execute strSQL
    22. Loop
    23.  
    24. Close #1

    The syntax for the Replace and Split functions may be a little off, but they should be close. If the column is a character field, be sure to surround it in single quotes, like arrFile(0) above.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    11

    Talking

    Thank you both for those fast replies! I owe you!!!

    Thank you and have a nice evening
    !!!

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