importing CSV to MS SQL table


I am using VS 2008 EXPRESS EDITION and MS SQL 2005 Express edition.
Why is text file handling so frustrating in

I have been trying to look up an example of code that would let me import CSV file to MS SQL table.
I want to create a form through which i can enter column names and then by clicking a button i can import respective csv file fields into sql table.

As a beginner i just got lost in the mumbo jumbo of connection strings etc. I could import a csv file in MS SQL using BCP. But how to do it in a vb windows application form by clikin a button in

I tried to look up in books , on net i just cudnt find one complete a beginner its hard to understand incomplete examples.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Mark WillsConnect With a Mentor Topic AdvisorCommented:
Probably for the same reason you are not getting inundated with responses - it gets kinf of open ended, and never ending...

It has been a while since I have needed to write any such code, normally just call a stored procedure in SQL and let it handle it. That might be an option. Pretty sure bcp is considered part of the SQL engine, so would be part of the express version as well (yep, you get the full "engine" in express).

Interesting allowing the column names to be entered - trying to create a mapping of sorts. There in lies part of the challenge. You will have to be able to read the CSV file first and display the columns as "quazzi" names, then allow mapping to "real" names, otherwise how are you going to link it up.

Assume the form is not the issue - if nothing else, it is just a table of two columns - one is populated with the names of the CSV file, the other is where you enter the real names (and destination table as well)...

So, continuing on a theme of not providing the full answer, There is a reasonable code snippet for handling the CSV file itself :
Have you seen : there is an option there to email the author as well...

The biggest difficulty is not all CSV's are created equal, so there are various degrees of success given the variety of combinations available - especially when you consider type casting as well.

There are more examples in C# - have you thought of that ? They can co-exist (with VB.Net), often find the intellectual "tricky" stuff has more examples in c#

Can write you a stored procedure to unpack into a temporary table - maybe the first 10 records, and return the columns, but not so good with the VB.Net these days...

use SQL management studio express ( and then you can import much easier into SQL express...

rogersamAuthor Commented:
Hi thanx for replying. I will try the stored procedure way.
The CSV files i will be dealing with will be pretty well defined. So that should not bew a proble.

I have to use express editions only thats a condition. I wish I cud use C# , but have to use VB only , thats anoother condition. Damn
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Mark WillsTopic AdvisorCommented:
OK, I will keep a watch, my e-mail is on my bio, or post back here...

just found another link on codeproject - that is where I normally go for inspiration - there are a few examples there as well...

rogersamAuthor Commented:
cant I use BCP?

i COULDNT FIND A WAY TO INSERT bcp CODE IN MY APPLICATION  code. It should work in the express editions.
rogersamAuthor Commented:
Also I need a relational database, thats one more condition coz i need to manipulate it.
and yea, as a beginner, code snippets make no sense to me atall....may be am missin something
Mark WillsTopic AdvisorCommented:
Yep, BCP is fine... Could also use openrowset and other bulk tools...
rogersamAuthor Commented:
CAN U enlighten me how to use BCP in a code ?!
rogersamAuthor Commented:
Alright heres the update, I am using (as conditions) VS 2008 express edition and SqlCe 3.5.

Now the toughest thing is, SSMS 2005 doesn't work with SqlCe 3.5. It gets better, SqlCe doesnot support BULK INSERT either.

So I am just importing the schema from a text file, creating a temp table and then importing data into it record by record. Then I modify the table according to the needs.

Lets see how succesful it is, I am not sure about formatting the text file, have to look into that.
Mark WillsTopic AdvisorCommented:
SQLCE or SSCE 3.5 ? they both use sdf, if created on mobile, then might be out of date.  Should be able to use (if latest build) on both desktop and mobile device - do you have all the latest downloads ?

SSMS 2005 will not support - might later, but need to load either VS 2008 or SQL2008 - the latter, you just need the management console, and would strongly suggest getting that.

However if your "design/ development" platform is in SQL 2005, then use that space to design in SQL 2005 and then migrate to SSCE 3.5 via

Also have a look at (with connecting to 3.5) :

Now, SQL Server 2008 Management Studio, is meant to be available and fully supports 3.5, but not yet as a seperate download. They have release the SQL 2008 Express with Advanced Services which does include it :


and all the way down the bottom there is a link to 2008 Express with Advanced Services. Alternatively, get the eval version of SQL Server 2008 and just install the client tools... Please note all this is still pre-release...

Also... Check and the links down the bottom...

1. SQL Server Compact 3.5 and Synchronization Services for ADO.Net v1.0 for Windows Desktop:
Installs the files for SQL Server Compact 3.5 and Microsoft Synchronization Services for ADO.Net for Windows Desktop platform

2. SQL Server Compact 3.5 Server Tools:
Installs the Server Tools on the IIS server enabling merge replication and remote data access (RDA) with SQL Server 2005 and later.

3. SQL Server Compact 3.5 Books Online and Samples:
The download contains SQL Server Compact 3.5 Books Online, Online Read Me and Samples. SQL Server Compact 3.5 Books Online do not install with Visual Studio 2008. The Books Online are only available on the Web.

4. Synchronization Services for ADO.Net v1.0 Books Online:
Synchronization Services provides the ability to synchronize data from disparate sources over two-tier, N-tier, and service-based architectures. The Books Online has more information about Synchronization Services. The forum for Synchronization Services provides technical Q&A about using Synchronization Services.


rogersamAuthor Commented:
Do I have to uninstall MS SQL 2005 EXPRESS EDITION and SSMS 2005 before I download MS SQL 2008 EXPRESS and SSMS 2008?
Mark WillsTopic AdvisorCommented:
no. think there is a link in MSDN that discusses it - might be worth doing a search in there...
rogersamAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.