日期:2014-05-18  浏览次数:20391 次

这是我写的查询文章, 但是查询单号,不行。看看错再哪了??
asp.net   代码如下


string   strConn   =   "server=localhost;database=My;uid=sa;pwd=123456 ";
SqlConnection   conn   =   new   SqlConnection(strConn);
conn.Open();
SqlParameter   sp   =   new   SqlParameter( "@Keyword ",SqlDbType.NVarChar,50);
sp.Value   =   " ' ";     //   这里假设查找包含单引号的文章
SqlCommand   comm   =   new   SqlCommand( "SearchArticle ",conn);
comm.CommandType=CommandType.StoredProcedure;
comm.Parameters.Add(sp);
DataGrid1.DataSource   =   comm.ExecuteReader();
DataGrid1.DataBind();
conn.Close();


//   下面是SQL存储过程


ALTER       procedure   SearchArticle
@Keyword   NVARCHAR(50)
AS

declare   @sql   nvarchar(1000)--   这里必须要使用@sql变量字串做命令,不要改
set   @sql   =   'select   *   from   article   where   title   like   ' '% '+@Keyword+ '% ' ' '
exec(@sql)    


为什么查找别的都正确,查“ '”   单引号就错,错再哪里呢??     采用命令参数应该可以自动处理单引号问题啊,     我想是存储过程错了吧。


------解决方案--------------------
sp.Value = " ' ' "; //用两个单引号试试 ' '.
因为 '特殊字符.