Solved

C# ODBC connection to an excel spreadsheet

Posted on 2013-06-30
3
1,364 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
Comment Utility
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
Comment Utility
Anyway, I would strongly suggest you to use Excel DataReader.


Regards.
0
 

Author Closing Comment

by:pclarke7
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

762 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

13 Experts available now in Live!

Get 1:1 Help Now