Results 1 to 6 of 6

Thread: Importing from file to database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Location
    Denmark
    Posts
    3

    Importing from file to database

    Hi,

    I am quite new to this forum and I have a question regarding data import from a simple text file to a database.
    I am trying to write a code that takes the text from the attached file and import it to my database. Can anybody help my?
    I would prefer if it could be imported in to one row in the database in stead of being spread out over multiple.

    Attachment 95425

    Kind regards,
    Lasse Jepsen

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Importing from file to database

    You can call File.ReadAllText to get the entire contents of a file as a String and then saving it to a database is the same as for any other String. If you don't know how to save a String to a database then you should follow the Database FAQ link in my signature and check out the .NET resources there to learn how to use ADO.NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Location
    Denmark
    Posts
    3

    Re: Importing from file to database

    Quote Originally Posted by jmcilhinney View Post
    You can call File.ReadAllText to get the entire contents of a file as a String and then saving it to a database is the same as for any other String. If you don't know how to save a String to a database then you should follow the Database FAQ link in my signature and check out the .NET resources there to learn how to use ADO.NET.
    Thank you for the reply. I will give it a go!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Importing from file to database

    The duplicate of this thread has been deleted.

    When posting new threads please follow our one topic, one thread, in one forum section philosophy. Multiple threads on the same topic will either be merged into one (if both have replies) or deleted outright.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Location
    Denmark
    Posts
    3

    Re: Importing from file to database

    Once again thank you for the reply. I have managed to convert my txt file to at string. If I try to import this string as a flat file with the SQL Server import software I can do so. I have tried to read through your tutorials, however, I am unable to suceed in importing the string to my database. Here is how my code looks like:

    Code:
    Dim connectionString As String =
        "Data Source=BÆRBAR\SQLEXPRESS;" &
        "Initial Catalog=TEST;" &
        "Integrated Security=True;Pooling=False"
            Dim linkToDB As New SqlConnection(
               connectionString)
            linkToDB.Open()
            Dim commandWrapper As New SqlCommand(
                    "BULK INSERT [import contact] FROM " & fileReader &
                    " WITH ( FIELDTERMINATOR = ';' , ROWTERMINATOR = '|' ) ", linkToDB)
            MsgBox(fileReader)
    My string looks like this:
    |
    9999;
    1347089590;
    0;
    906000;
    B;
    ;
    false;
    0;
    0;
    0;
    0;
    ;
    Hejsa;
    2;
    Non-MILCO;
    false;
    No info;
    0;
    0;
    0;
    ;
    Not Dealt With;
    0.975164456623246;
    0.196728754225765;
    2;
    Non-MILCO;
    1.1;
    Can anybody help me to get this corrected?

    Kind regards

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Re: Importing from file to database

    i think you need to call file.ReadAllText to get the entire contents of a file as String and then saved it to a DB.

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