解析查询时出错

时间:2011-12-09 02:19:12

标签: c# .net sql-server-ce

错误:

[token line number = 1,token line offset = 15,token in error = %]

我的代码:

 SqlCeCommand cmd = con.CreateCommand();
 cmd.Connection = con;
 cmd.CommandText = "select emails from contacts where name like %@name";
 cmd.Parameters.AddWithValue("@name", name);
 SqlCeDataReader data = cmd.ExecuteReader();
 //.. 

我相信这是因为查询中的@name。 我也试过了:select emails from contacts where name like '%@name'

我是如何解决的?谢谢!

2 个答案:

答案 0 :(得分:2)

您需要在SQL解析器中隐藏%,因为它是字符串文字的一部分:

SqlCeCommand cmd = con.CreateCommand();
cmd.Connection = con;
cmd.CommandText = "select emails from contacts where name like @name";
cmd.Parameters.AddWithValue("@name", "%"+name);
SqlCeDataReader data = cmd.ExecuteReader();

答案 1 :(得分:1)

你应该试试这个:

select emails from contacts where name like '%'+@name