Solved

C# ODBC connection to an excel spreadsheet

Posted on 2013-06-30
3
1,368 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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now