views:

70

answers:

1

I'm using SQL Server 2005, VB.NET 2005. I want to be able to import a very large excel file into a SQL table called "XYZ"

I've done this by doing the following:
1. Save the excel file as csv.(Using SaveAs XLCSV option)
2. Build a datatable "ABC" From CSV.(using ODBC Connection and Select * from '*'.csv command)
3. copy the datatable"ABC" into database table "xyz" (using sqlBulkCopy.WriteToServer())

It works fine without any error but when i checked my database i found that data type for some columns has been changed and hence it didn't copy some of the records.Any help would be appreciated

A: 

I think you would be better off creating the target table in SQL server and using T-SQL BULK INSERT to insert the CSV. The data types are then explicit in your table. Using an intermediate database runs the risk of inferring the wrong datatype, as well as redundantly copies the data.

mdma
I am not using intermediate database its just dataset which gets fill from csv using odbc dataadapter.My target table is in Sql Server only.
Swapnil
Sorry - I misread "datatable" as "database". Makes sense now! In your SELECT * FROM ABC, try selecting columns explicitly and using (CAST <columnname> AS type) to explicitly name the type to use.
mdma
Could you please help me with writing such sql statement, thanks.
Swapnil
It may be simplest that you create a schema.ini file and place that in the same folder as your CSV. see http://msdn.microsoft.com/en-us/library/ms709353%28VS.85%29.aspx
mdma
Thanks A lot , That helped.Altough i have one more question , how can i avail the schema file for multiple csv file in same directory. is it possible to write the column name and format for multiple csv file in same Schema file??
Swapnil
If all the files have the same format, then you can use the same file. Otherwise the csv file will have to go in separate directories. If you are sure that all data in your excel columns are consistent (e.g. no mixed data types) then you can use the excel driver, which doesn't need schema.ini. See here for why mixed types are bad with the excel driver. http://www.yorkspace.com/2006/03/37
mdma
Thats exactly what i am facing in here :).. thanks for your help.
Swapnil