|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Formatting a SQL queryI have the following query written for MS Access: PARAMETERS [Enter a keyword to search] Text ( 255 ); SELECT TOP 10 ISBN, Title FROM Books WHERE Keywords LIKE "*" & [Enter a keyword to search] & "*" ORDER BY Title; This query works as expected. Now, how do I write this query in VB.NET for an OleDBCommand's text property? I tried this but it doesn't seem to be working: "Select Top 10 ISBN, Title FROM Books WHERE Keywords Like '*' & ? & '*' ORDER BY Title ASC;" Any thoughts? Thanks, Roshawn Try:
"Select Top 10 ISBN, Title FROM Books WHERE Keywords Like '*" & InputBox("Enter a keyword to search") & "*' ORDER BY Title ASC;" Travis Murray Artiem Consulting, Inc. http://www.artiem.com Show quoteHide quote "Roshawn Dawson" <udr***@bellsouth.net> wrote in message news:ezjNr0XOFHA.576@TK2MSFTNGP15.phx.gbl... > Hi, > > I have the following query written for MS Access: > > PARAMETERS [Enter a keyword to search] Text ( 255 ); > SELECT TOP 10 ISBN, Title > FROM Books > WHERE Keywords LIKE "*" & [Enter a keyword to search] & "*" > ORDER BY Title; > > This query works as expected. Now, how do I write this query in VB.NET > for an OleDBCommand's text property? I tried this but it doesn't seem to > be working: > > "Select Top 10 ISBN, Title FROM Books WHERE Keywords Like '*' & ? & '*' > ORDER BY Title ASC;" > > Any thoughts? > > Thanks, > Roshawn Roshawn - what do you mean by doesn't seem to be working? Is it throwing an
exception? If so the likely culprit is that you aren't adding the parameters to your OleDbCommand's paramaters collection ie http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbparameterclasstopic.asp -- Show quoteHide quoteW.G. Ryan MVP (Windows Embedded) TiBA Solutions www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com "Roshawn Dawson" <udr***@bellsouth.net> wrote in message news:ezjNr0XOFHA.576@TK2MSFTNGP15.phx.gbl... > Hi, > > I have the following query written for MS Access: > > PARAMETERS [Enter a keyword to search] Text ( 255 ); > SELECT TOP 10 ISBN, Title > FROM Books > WHERE Keywords LIKE "*" & [Enter a keyword to search] & "*" > ORDER BY Title; > > This query works as expected. Now, how do I write this query in VB.NET > for an OleDBCommand's text property? I tried this but it doesn't seem > to be working: > > "Select Top 10 ISBN, Title FROM Books WHERE Keywords Like '*' & ? & '*' > ORDER BY Title ASC;" > > Any thoughts? > > Thanks, > Roshawn Thanks for your reponses.
Travis - I won't be using an InputBox. The parameter's value will acutally be a querystring value. Ryan - I have indeed added a parameter to the OleDBCommand's parameter collection. No errors are generated at all. At first I was getting a syntax error, but the current rewrite of the sql string fixed that. Any more ideas? Thanks, Roshawn Hi Roshawn,
In case you are not getting the desired output, try using % in place of *. Wild character is % while going through .NET. Hope this helps. Thanks, Jag. You are a hair-saver and money-saver (you saved me from
pulling out my remaining hairs, thus saving me from buying a hairpiece). Be Cool Jag wrote: Show quoteHide quote > Hi Roshawn, > > In case you are not getting the desired output, try using % in place of > *. > Wild character is % while going through .NET. > > Hope this helps. > Hi,
Your could should use parameterized query. It should be something like (assuming that you have defined OledbConnection connection) Dim lcSQL as string="SELECT TOP 10 ISBN, Title FROM Books WHERE Keywords LIKE ? ORDER BY Title" Dim testCMD As OledbCommand = New OledbCommand (lcSQL , PubsConn) Dim loLikeParam As OledbParameter = testCMD.Parameters.Add ("@LikeParam", OledbType.VarChar, 20) loLikeParam.Value="Place my value here" Now you could execute cxommand to get DataTable or to open reader Show quoteHide quote "Roshawn Dawson" <udr***@bellsouth.net> wrote in message news:ezjNr0XOFHA.576@TK2MSFTNGP15.phx.gbl... > Hi, > > I have the following query written for MS Access: > > PARAMETERS [Enter a keyword to search] Text ( 255 ); > SELECT TOP 10 ISBN, Title > FROM Books > WHERE Keywords LIKE "*" & [Enter a keyword to search] & "*" > ORDER BY Title; > > This query works as expected. Now, how do I write this query in VB.NET > for an OleDBCommand's text property? I tried this but it doesn't seem to > be working: > > "Select Top 10 ISBN, Title FROM Books WHERE Keywords Like '*' & ? & '*' > ORDER BY Title ASC;" > > Any thoughts? > > Thanks, > Roshawn
Other interesting topics
Schema Collection -> Strongly Typed Dataset
Dataset Merge Question Multiple DataTables in Dataset MS Best Practice - Load subset of a single Row Transactions in ADO.Net 2 strongly-typed datsets. Cannot Open Any More Tables multiple tables in a flat grid Editing Info in a DataTable ReadXml into a DataTable? Retrieving datarows using DataTable.Select method and adding it to new DataTable |
|||||||||||||||||||||||