0

I am having difficulty in constructing SQL Commands in VB.Net wherein my project is connected to Excel via OLEDB.

Select * From [Sheet$]

This SQL Command works but whenever I try to add another SQL Command WHERE so that I could filter the data and I only wanted to be shown. I have headers in my excel file, the Name header is the 4th column equivalent to Column letter: D.

Select * From [Sheet$] Where Name='Xander'

it doesn't work, it shows nothing. What is the correct 'WHERE' SQL Command for Excel to OLEDB Connection?

By the way this is the code I am working on

 Dim conn As OleDbConnection
 Dim adapt As OleDbDataAdapter
 Dim dts As DataSet
 Dim excel As String = My.Settings.DefaultDirectory

 conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
 conn.Open()

 adapt = New OleDbDataAdapter("Select * from [Sheet$] where NAME='Xander'", conn)
 dts = New DataSet

 adapt.Fill(dts, "[Sheet$]")
 DataGridView1.DataSource = dts
 DataGridView1.DataMember = "[Sheet$]"

 conn.Close()
6
  • What is your connectionstring and do you have headers columns? Commented May 12, 2018 at 13:40
  • Is CellHeader the header/column? Commented May 12, 2018 at 13:48
  • Edited the post. @Codexer yes that it is the header/column Commented May 12, 2018 at 13:54
  • 1
    Try Select * from [Sheet$] Where [NAME]='Xander' Commented May 12, 2018 at 13:56
  • @Codexer, holy groot friend! Thank you, I havent thought of adding brackets in the Header Name thank you. Kindly write the answer down so I could mark it as Solved/Answered. Thank you again. Commented May 12, 2018 at 14:01

1 Answer 1

1

You need to enclose brackets around your column names to escape them. Also Name is a reserved word, so you must escape it.

 Select * from [Sheet$] Where [NAME]='Xander'
Sign up to request clarification or add additional context in comments.

4 Comments

follow up question, how about [DATE]='4/2/2017' I think it is working with letter characters but not with Date format.
Did I do it right? Select * from [Q2$] Where [DATE]='#3/2/2018'
Select * from [Q2$] Where [DATE]=#3/2/2018# this is how you do it... you need to remove ticks and wrap date in #...
Man you're a lifesaver, I'm kinda new in Excel SQL coz I only get used to Database SQLs, but man Thank you. I think this is all for now..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.