Welcome to Dream.In.Code
Click Here
Getting C# Help is Easy!

Join 118,309 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,696 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



C# with Excel

 
Reply to this topicStart new topic

C# with Excel, Querying excel in C#

seethasaty
post 5 Aug, 2008 - 01:55 PM
Post #1


New D.I.C Head

*
Joined: 31 Jul, 2008
Posts: 2


My Contributions


Hi,

I have an excel sheet with Date as one of the field. The format of date in excel sheet is dd/mm/yyyy. I have connected C# with excel through OLEDB. My code is,

string excel_connection_string = @"Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=C:\Seetha's\Projects\StockAnalysis\Lloyds.xls;Extended Properties=Excel 8.0;";
OleDbDataAdapter exceladapter = new OleDbDataAdapter("Select * from [st$]", excel_connection_string);
DataSet ds = new DataSet();
exceladapter.Fill(ds, "StockInfo");
Grid1.DataSource = ds.Tables["StockInfo"].DefaultView;
Grid1.DataBind();


This works perfectly. But problem comes when i try to use Date field in the 'where' clause of Sql query. If i use the query
OleDbDataAdapter exceladapter = new OleDbDataAdapter("Select * from [st$] where convert(varchar(19), StockDate, 120) between '2008-01-01' and '2008-07-30'", excel_connection_string);
where StockDate is the date field in the excel sheet.

It shows oledbexception as Undefined function 'convert' in expression.
I want the values from excel sheet within the given range of date. Please help.
User is offlineProfile CardPM

Go to the top of the page


kikz
post 5 Aug, 2008 - 03:48 PM
Post #2


New D.I.C Head

*
Joined: 21 Jul, 2008
Posts: 32



Thanked 1 times
My Contributions


Convert is a sql server function (and maybe others), but not an Excel function? What does the 120 do? Do you mean to add 120 days to StockDate? It seems you're trying to convert StockDate to a varchar. Why not convert it to a DateTime to do the between test.
User is offlineProfile CardPM

Go to the top of the page

seethasaty
post 6 Aug, 2008 - 07:11 AM
Post #3


New D.I.C Head

*
Joined: 31 Jul, 2008
Posts: 2


My Contributions


Thanks yaar.... convert is an sql function. I just changed the format.

OleDbDataAdapter exceladapter = new OleDbDataAdapter("Select * from [st$] where [StockDate] between #01/06/2008# and #30/06/2008#", excel_connection_string);

It worked !!!!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/10/08 11:44AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month