CSV Import

Posted on 2012-09-02
Last Modified: 2012-09-19
I am importing a csv file into my Database. It is a comma delimitered file.
I have 2 tables

tPhonebook  - ID, sBook
tContacts  -  ID, sName, sPhone, iPhonebookID

iPhonebookID is linked to ID of tPhonebook

The csv file consists of

Column 1 - Name
Column 2 - Number

Column 3, 4, 5 etc is the name of a Phonebook.

I need the import to add a phone book for every column named after column 2, it could be one or multiple columns, it is different for every row.

I then need it to add that contact to each phone book and move to the next row.

If the phone book exists already I just need to ID for iPhonebookID.
Question by:mgordon-spi
    LVL 12

    Expert Comment

    by:Habib Pourfard
    Would you please give us a sample record of the csv file. is it like this?

    Name, Number, Col1, Col2, Col3
    John, 123456, Phonebook1, NULL, NULL
    John, 123456, NULL, Phonebook2, NULL
    LVL 16

    Expert Comment

    by:Kalpesh Chhatrala
    try below CSV Import Sample



    Author Comment

    The csv would be like this

    John, 123456, Phonebook1,  
    John, 123456, Phonebook1, Phonebook2, Phonebook3,
    John, 123456, Phonebook1, Phonebook2,

    if there is only one phone book there is only 3 columns if there is 2 phone books there is 4 columns and so on. There would never be a null column and then a phone book column
    LVL 83

    Accepted Solution

    That's not a properly formatted CSV file so you would need to read the file line by line, break the line on comma and then deal with the outcome manually

    ForEach Line As String In IO.File.ReadAllLines("path")
          Dim words As String() = Line.Split(",")
          For each word As String in words
                'code here

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Viewers will learn how the fundamental information of how to create a table.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now