2009-03-20 5 views
48

J'ai le code suivant:Utilisation de SqlParameter à la clause SQL LIKE ne fonctionne pas

const string Sql = 
    @"select distinct [name] 
     from tblCustomers 
     left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId 
     where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');"; 

using (var command = new SqlCommand(Sql, Connection)) 
{  
    command.Parameters.AddWithValue("@SEARCH", searchString); 
    ... 
} 

Cela ne fonctionne pas, j'essayé ainsi:

const string Sql = 
    @"select distinct [name] 
    from tblCustomers 
    left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId 
    where (tblCustomer.Name LIKE @SEARCH OR tblCustomerInfo.Info LIKE @SEARCH);"; 

using (var command = new SqlCommand(Sql, Connection)) 
{  
    command.Parameters.AddWithValue("@SEARCH", "'%" + searchString + "%'"); 
    ... 
} 

mais cela ne fonctionne pas aussi bien . Qu'est-ce qui ne va pas? Aucune suggestion?

Répondre

85

Qu'est-ce que vous voulez est:

tblCustomerInfo.Info LIKE '%' + @SEARCH + '%' 

(ou modifier la valeur du paramètre à inclure le% en premier lieu).

Sinon, vous êtes (premier exemple) recherchant le littéral "@SEARCH" (pas la valeur arg) ou vous incorporez des guillemets supplémentaires dans la requête (second exemple).

D'une certaine façon, il pourrait être plus facile d'avoir le TSQL il suffit d'utiliser LIKE @SEARCH, et le manipuler à l'appelant:

command.Parameters.AddWithValue("@SEARCH","%" + searchString + "%"); 

deux approches devrait fonctionner.

+0

command.Parameters.AddWithValue (» @SEARCH ","% "+ searchString +"% "); Travaillé, les guillemets simples supplémentaires ont été le problème comme vous l'avez souligné – Ngm

+1

Le faire de la deuxième manière me semble meilleur parce que la déclaration finale semblera en effet soignée. – Javid

+8

Cela ne fonctionnera pas correctement lorsque searchString contient les caractères '%' '_' ou' [', en supposant que vous voulez réellement rechercher l'un de ces littéraux de charater. Pour une solution à 100%, vous devez placer ces caractères dans la chaîne searchString entre parenthèses '[]'. Le code C# 'Regex.Replace (searchString, @" ([% _ \ []) ", @" [$ 1] ")' fait l'affaire. –

-1

Vous pouvez faire LIKE @SEARCH et dans votre code C#, faire

searchString = "%" + searchString + "%" 
+4

cela n'exposerait-il pas une opportunité pour l'injection de sql? –

+0

Oui, il le ferait. Ou, peut-être plus probable, quelqu'un va chercher une chaîne contenant un 'personnage et il va exploser. Ne faites pas cela. – Taran

3

Au lieu d'utiliser:

const string Sql = 
@"select distinct [name] 
    from tblCustomers 
    left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId 
    where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');"; 

Utilisez ce code:

const string Sql = 
@"select distinct [name] 
    from tblCustomers 
    left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId 
    where (tblCustomer.Name LIKE '%' + @SEARCH + '%' OR tblCustomerInfo.Info LIKE '%' + @SEARCH + '%');"; 
+0

Cela n'a aucun sens pour moi mais ça marche. Pourriez-vous expliquer pourquoi? – Timothy

Questions connexes