Access, LIKE in query con parametro lanciata da vb

giovedì 12 marzo 2009 - 09.22

volperubbia Profilo | Senior Member

Ciao a tutti,
su un mdb ho la seguente query "uspSearchAuthors":

PARAMETERS [@SearchKey] Text ( 255 );
SELECT Authors.ID, (Trim([Firstname])+' '+Trim([Lastname])) AS Description
FROM Authors
WHERE (Trim([@SearchKey])<>"") And
(UCase(Trim([Firstname])) Like UCase(Trim([@SearchKey])) Or
UCase(Trim([Lastname])) Like UCase(Trim([@SearchKey])) Or
(UCase(Trim([Firstname]))+' '+UCase(Trim([Lastname]))) Like UCase(Trim([@SearchKey])) Or
(UCase(Trim([Lastname]))+' '+UCase(Trim([Firstname]))) Like UCase(Trim([@SearchKey])))
ORDER BY Trim([Firstname]), Trim([Lastname]);

Se la lancio da mdb con il parametro "ma*", ottengo N righe,
se la lancio da vb ottenngo invece 0 righe.

Dim SearchKey As String = "ma*"
Dm tbl As New DataTable
Dim ConnectionString As String = "......"
Dim Cmd As New System.Data.OleDb.OleDbCommand("uspSearchAuthors", New System.Data.OleDb.OleDbConnection(ConnectionString))
Cmd.CommandType = CommandType.StoredProcedure
Cmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@SearchKey", SearchKey))
Dim Adp As New System.Data.OleDb.OleDbDataAdapter(Cmd)
Adp.Fill(tbl)

Qualche idea sul perchè? E' il LIKE che da fastidio?

Grazie per l'attenzione,
Davide

lbenaglia Profilo | Guru

>Qualche idea sul perchè? E' il LIKE che da fastidio?

Ciao Davide,

Se esegui la query via ADO, sostituisci l'* con il %.
Inoltre Access è case insensitive, pertanto tutti quei UCase non servono.
Infine, sei sicuro che i Trim siano veramente necessari?

>Grazie per l'attenzione
Prego.

Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org

volperubbia Profilo | Senior Member

Hai ragione Lorenzo
eseguendo la query via vb con ADO, bisogna sostituire l'* con il %,
non ci avevo pensato ...

Davide

lbenaglia Profilo | Guru

>Hai ragione Lorenzo
>eseguendo la query via vb con ADO, bisogna sostituire l'* con
>il %,
>non ci avevo pensato ...

La query

PARAMETERS [@SearchKey] Text ( 255 ); SELECT Authors.ID, (Trim([Firstname])+' '+Trim([Lastname])) AS Description FROM Authors WHERE (Trim([@SearchKey])<>"") And (UCase(Trim([Firstname])) Like UCase(Trim([@SearchKey])) Or UCase(Trim([Lastname])) Like UCase(Trim([@SearchKey])) Or (UCase(Trim([Firstname]))+' '+UCase(Trim([Lastname]))) Like UCase(Trim([@SearchKey])) Or (UCase(Trim([Lastname]))+' '+UCase(Trim([Firstname]))) Like UCase(Trim([@SearchKey]))) ORDER BY Trim([Firstname]), Trim([Lastname]);

La riscriverei come:

PARAMETERS [@SearchKey] Text ( 255 ); SELECT ID, Firstname + ' ' + Lastname AS Description FROM Authors WHERE Firstname LIKE @SearchKey OR Lastname LIKE @SearchKey ORDER BY Firstname, Lastname;

Avendo cura di utilizzare il % sia come prefisso che come suffisso alla tua chiave di ricerca nel caso voglia restituire anche le righe che soddisfano le sottostringhe nel nome e cognome.

Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org

volperubbia Profilo | Senior Member

Giustissimo Lorenzo ... avevo già ripulito la query,
mi sono scordato di darti conferma ...

Grazie
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5