|
-
Jul 31st, 2010, 11:53 AM
#1
Thread Starter
Hyperactive Member
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?
-
Jul 31st, 2010, 08:54 PM
#2
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.
-
Aug 2nd, 2010, 08:11 AM
#3
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|