Solved

C# ODBC connection to an excel spreadsheet

Posted on 2013-06-30
3
1,374 Views
Last Modified: 2013-07-01
Hello,
I am using an ODBC connection to link to an excel spreadsheet in C#. the Excel driver in version 12 (xls, xlsx, xlsm, xlsb).  If I use an SQL statement such as:  

"Select * from [transaction_master$] " 

then I can successfully iterate through the spreadsheet rows. However when I try to use a statement such as:


Select * from [transaction_master$] WHERE  T2_CompanyID = " +  MyCompanyNo "   

Where T2_CompanyID is a column heading on the spreadsheet, then I get the following error:

ERROR [22018] [Microsoft][ODBC Excel Driver] Data type mismatch in criteria expression.

The connection string is set to:

DSN=RulesdbExcel;Data Source=C:\\C# Folder\\RulesdbExcel.xls; HDR=yes; IMEX=1; Format=xls;

Any idea what is causing this and how I might get around it ?

regards
Pat
0
Comment
Question by:pclarke7
3 Comments
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 250 total points
ID: 39288576
Hi Pat,

Given that you are using a value of 1 (ImportMixedType) for the IMEX ("intermixed") option, does the column [T2_CompanyID] contain alphanumeric or numeric data?

If alphanumeric you will need to include the search criteria in your SQL statement in (double) quote characters; ASCII Character 34 (").

In Visual Basic for Applications, for instance, this may be achieved as follows;
Chr$(34) & MyCompanyNo & Chr$(34)

BFN,

fp.
0
 
LVL 13

Accepted Solution

by:
jonnidip earned 250 total points
ID: 39289004
Anyway, I would strongly suggest you to use Excel DataReader.


Regards.
0
 

Author Closing Comment

by:pclarke7
ID: 39290869
Hi Guys,
thanks for your input. I will definitely check out Excel DataReader.

However thanks to your comments I found the problem. The Spreadsheet had one of the rows used in the SQL as alpha when it should have been numeric. Once I changed this it worked.

regards
Pat
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question