public List<Notice> GetAllLike(string title)
        {
            //string sql = "select  * from notice where notice.NoticeTitle like '%" + title + "% 'order by notice.PostDate desc  ";            string sql = "select * from notice where notice.NoticeTitle like '%@NoticeTitle%' order by notice.PostDate desc ";
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlParameter par = new SqlParameter("@NoticeTitle", title);
            comm.Parameters.Add(par);
            SqlDataReader reader = comm.ExecuteReader();
            List<Notice> list = new List<Notice>();
            try
            {
                while (reader.Read())
                {
                    Notice notice = new Notice();
                    notice.Id = Convert.ToInt32(reader["Id"]);
                    notice.NoticeTitle = reader["NoticeTitle"].ToString();
                    notice.NoticeContent = reader["NoticeContent"].ToString();
                    notice.PostName = reader["PostName"].ToString();
                    notice.PostTime = Convert.ToDateTime(reader["PostDate"]);
                    notice.NoticeDepartment = reader["NoticeDepartment"].ToString();
                    list.Add(notice);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                reader.Close();
                conn.Close();            }
            return list;        }

解决方案 »

  1.   

    你把正确的东西注释掉,写个不正确的出来,不知道什么原因?!如果要拼接title变量,记着符合t-sql语法的写法应该对单引号转义,例如:string sql = "select  * from notice where notice.NoticeTitle like '%" + title.Replace("'","''")
             + "% 'order by notice.PostDate desc  ";
      

  2.   

    谢谢。但是代码根本就不读取while里面的数据! 所以返回总是空值。。为什么不读取呢?? 
      

  3.   

    string sql=string.format("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc",title);
      

  4.   

      public DataSet GetAllLike(string title)
            {
                //string sql = "select  * from notice where notice.PostDate like '%" + title + "%' order by notice.PostDate desc  ";
                string sql = String .Format ("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc", title); 
                conn.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);      
                da.Fill(ds, "notice");
                conn.Close();
                return ds;
            }
    这样写对吗? 不过还是空值 我都愁死了
      

  5.   

     string sql = String .Format ("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc", title); 
      string sql = String .Format ("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc", ‘%"+title+”%’); 
    ???这样吗  string sql = String .Format ("select * from notice where notice.NoticeTitle like order ‘%"+title+”%’ by notice.PostDate desc); 
      

  6.   

                        SqlParameter[] sp = new SqlParameter[1];
                        sp[0] = new SqlParameter("?aaa", SqlDbType.VarChar, 200);                    sp[0].Value ="%" +"1"+"%";
                        DataTable dt = SqlHelper.ExecuteDataTable(connection, CommandType.Text, "SELECT * FROM T WHERE TID LIKE ?aaa", sp);
    给你推荐一个写法
    我这个是mysql的你MS SQL的也差不多只是?换成@
      

  7.   

    他这个不行,如果你存储的是一些特殊字符那就真的遗笑大方了,SQL注入怎么来的?我在外部过滤?
    那也不是完全之策,所以我推荐我上面的写法。
      

  8.   

    可以不过用他的方式就不可以,必须用我的方法写才行,或者用存储过程,其实原理一样,
    举个例子
    你的{0} 是--
    你的SQL语句就是select * from notice where notice.NoticeTitle like -- order by notice.PostDate desc
    order by就会被注释掉,而like后语法就错误了,程序会立刻报错。