首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

sql模糊查询为啥返回时空值? 急

2012-12-15 
sql模糊查询为什么返回时空值?? 急急急!! public ListNotice GetAllLike(string title){//string sql

sql模糊查询为什么返回时空值?? 急急急!!
 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;

        }
[最优解释]
你把正确的东西注释掉,写个不正确的出来,不知道什么原因?!

如果要拼接title变量,记着符合t-sql语法的写法应该对单引号转义,例如:

string sql = "select  * from notice where notice.NoticeTitle like '%" + title.Replace("'","''")
         + "% 'order by notice.PostDate desc  ";

[其他解释]
另外要知道,这种用不到索引的傻瓜化遍历方法,只有在万不得已的时候才使用。或者(比如说)产品的客户是很小的单位、根本不可能提出性能要求的时候才使用。

正常的数据库程序设计,不会滥用查询,一定要懂得使用索引。
[其他解释]
 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); 

[其他解释]
引用:
你把正确的东西注释掉,写个不正确的出来,不知道什么原因?!

如果要拼接title变量,记着符合t-sql语法的写法应该对单引号转义,例如:


C# code?



12

string sql = "select  * from notice where notice.NoticeTitle like '%" + title.Replace("'","''")     ……

你这样如果要查找'怎么弄?
[其他解释]
引用:
select * from notice where notice.NoticeTitle like '%@NoticeTitle%' order by notice.PostDate desc


这时哪家sql语法呢?
[其他解释]
引用:
引用:select * from notice where notice.NoticeTitle like '%@NoticeTitle%' order by notice.PostDate desc

这时哪家sql语法呢?

那应该怎么写??? 
[其他解释]
引用:
另外要知道,这种用不到索引的傻瓜化遍历方法,只有在万不得已的时候才使用。或者(比如说)产品的客户是很小的单位、根本不可能提出性能要求的时候才使用。

正常的数据库程序设计,不会滥用查询,一定要懂得使用索引。

谢谢。但是代码根本就不读取while里面的数据! 所以返回总是空值。。为什么不读取呢?? 
[其他解释]
没人回复啊。。等待中 
[其他解释]
string sql=string.format("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc",title);


[其他解释]
  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;
        }
这样写对吗? 不过还是空值 我都愁死了
[其他解释]
                    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的也差不多只是?换成@
[其他解释]

引用:
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.N……

他这个不行,如果你存储的是一些特殊字符那就真的遗笑大方了,SQL注入怎么来的?我在外部过滤?
那也不是完全之策,所以我推荐我上面的写法。
[其他解释]
引用:
引用:
string sql = String .Format ("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc", title); 
 

 string sql = String .Format ("select * fro……


特殊字符不可以索引??例如 楼上解释下  我是刚刚学c#的
[其他解释]
引用:
引用:
引用:
string sql = String .Format ("select * from notice where notice.NoticeTitle like '{0}'order by notice.PostDate desc", title); 


 

 string sql = Strin……


可以不过用他的方式就不可以,必须用我的方法写才行,或者用存储过程,其实原理一样,
举个例子
你的{0} 是--
你的SQL语句就是

select * from notice where notice.NoticeTitle like -- order by notice.PostDate desc
order by就会被注释掉,而like后语法就错误了,程序会立刻报错。

热点排行