Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1487
  • Last Modified:

C# ODBC connection to an excel spreadsheet

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
pclarke7
Asked:
pclarke7
2 Solutions
 
[ fanpages ]IT Services ConsultantCommented:
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
 
jonnidipCommented:
Anyway, I would strongly suggest you to use Excel DataReader.


Regards.
0
 
pclarke7Author Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now