Results 1 to 3 of 3

Thread: Import .csv into SQL Server 2008

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Import .csv into SQL Server 2008

    I need to import a .csv file into a table in SQL Server.

    I need for the user to be able to select the file and then click to import that file.

    What is the best way to go about this?

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

    Re: Import .csv into SQL Server 2008

    You would use an OpenFileDialog to allow the user to select the file.

    You can then use standard ADO.NET to read the file into a DataTable using an OleDbDataAdapter. You'll then want to validate the contents of the DataTable to make sure it complies with your database schema.

    Finally, you would use the SqlBulkCopy class to insert the data into SQL Server.

    Each of the preceding paragraphs is a distinct step and you should treat them that way. Work on one step at a time and only go on to the next when you complete the previous one. Your first order of business is the user selecting the file with an OpenFileDialog. If you don't know how to use it then you should start by reading the MSDN documentation for the class. You can then search for examples on the web if you need them. If you still need help after that, post back here and show us what you've done so far.

    Once that's done, you should check out the Database FAQ link in my signature for the ADO.NET part. Finally, do the same for the SqlBulkCopy class as you did for the OpenFileDialog class: read the documentation, search for examples, do what you can for yourself and then post back with what you've got if you have issues.
    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
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Import .csv into SQL Server 2008

    Thank you for that. That is really helpful and clearly set out.

    I am familiar with, OpenFileDialog, but the SqlBulkCopy I have not used before so that is going to be very helpful.

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