chetan1539
chetan1539

Reputation: 121

Issue with Importing xls file to sql server 2008 using c#

I am doing the task of importing xls file to sql server 2008 using c#, the xls file contains 3 column (ProductCode = having alphanumeric values,Productname = having string values, Categoryids = having alphanumeric values) in xls file.

When I am importing the xls through my code it reads Productname,Categoryids but ProductCode with only numeric values, it can not read the codes which containing characters.

eg : sample column values productcode -30-sunscreen-250ml, 04 5056, 045714PC, 10-cam-bag-pouch-navy-dot, 100102

I reads 100102, but it can not reads the [045714PC,04 5056,-30-sunscreen-250ml,10-cam-bag-pouch-navy-dot]

Please suggest any solutions.

Thanks

Upvotes: 0

Views: 441

Answers (1)

richardtallent
richardtallent

Reputation: 35363

Excel's OLEDB driver makes assumptions about the column's data based on the first 8 rows of data. If the majority of the first 8 rows for a given column, it assumes the entire column is numeric and then can't properly handle the alphanumeric values.

There are four solutions for this:

  1. Sort your incoming data so the majority of the first 8 rows have alphanumeric values in that column (and in any other column with mixed numeric / alphanumeric data).

  2. Add rows of fake data in, say, rows 2-9 that you ignore when you actually perform the import, and ensure that row contains letters in any column that should not be strictly numeric.

  3. Edit the REG_DWORD key called "TypeGuessRows" located at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel] in your registry and change the 8 to a 0. This will force Excel to look through the entire sheet before guessing the column data types. However, this can hinder performance. (You can also change the value from 8 to anything between 1 and 16, but that just changes how many rows Excel looks at, and 16 may still not be enough for you.)

  4. Add ";IMEX=1" in your connection string. This will change the logic to look for at least one non-numeric value instead of looking at the majority of the values. This may then be combined with solution (1) or (2) to ensure it "sees" an alphanumeric value in the appropriate columns within the first 8 rows.

Upvotes: 2

Related Questions