2015-12-28 70 views
-1

我有一个变量fav,它将包含用逗号分隔的值,例如“a,b,c”。在Table1我有一个名为Name的列,在不同行中的值为“a”“b”“c”。我想为下面的查询返回a,b和c,但它当前不返回并且不会崩溃。谢谢你的帮助。Sqlite WHERE LIKE语句c#

public IEnumerable<Table1> Method1() 
{ 
    return conn.Query<Table1>("SELECT * FROM [Table1] WHERE [Name] LIKE '" + "%" + fav + "%" + "'"); 
} 
+1

需要拆分逗号分隔字符串,然后使用IN子句与表值参数... –

回答

0

你需要使用“IN”,而不是“喜欢”

public IEnumerable<Table1> Method1() 
    { 
     string StrSql = "Select * From [Table1] Where [Name] in ("; 
     List<string> par = fav.Split(',').ToList(); 
     foreach (string _par in par) 
     { 
      StrSql += "'" + _par + "'"; 
      if (_par != par.Last()) 
      { 
       StrSql += ','; 
      } 
      else 
      { 
       StrSql += ")"; 
      } 
     } 
     return conn.Query<Table1>(StrSql); 
    } 
+1

字符串值需要引用。或使用参数。 –