?
Solved

C# ODBC connection to an excel spreadsheet

Posted on 2013-06-30
3
Medium Priority
?
1,435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 750 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 750 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

800 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