本帖最后由 宋兴征 于 2013-3-30 22:50 编辑
在做搜索功能时,SqlDataAdapter填充DataSet对象ds,但是发现ds.Tables[0].Rows.Count=0
也就是没有返回结果,但是sql语句在Management Studio中执行没错。
请大家帮忙看下!
搜索按钮的点击事件:- protected void imgBtn_Search_Click(object sender, ImageClickEventArgs e)
- {
- List<string> listWhere = new List<string>();
- List<SqlParameter> parameters = new List<SqlParameter>();
- if (ddlModule.SelectedIndex >= 0)
- {
- int pid = Convert.ToInt32(ddlModule.SelectedValue);
- listWhere.Add("pid=@pid");
- parameters.Add(new SqlParameter("@pid", pid));
- }
- if (txtRight.Text.Length > 0)
- {
- string rightName = txtRight.Text;
- listWhere.Add("Name like @Name");
- parameters.Add(new SqlParameter("@Name", "'%"+rightName+"%'")); //这里字符串的拼接有问题没有?
- }
- string str = string.Join(" and ", listWhere); //每个搜索条件以and分割开
- string sql = "select * from T_Right";
- if (listWhere.Count > 0)
- {
- sql = sql + " where " + str;
- }
- DataTable dt = SQLHelper.ExecuteDataTable(sql, parameters.ToArray());
- int rows = dt.Rows.Count; //rows=0,实在不明白
- List<Right> list = new List<Right>();
- if (dt.Rows.Count > 0)
- {
- foreach (DataRow row in dt.Rows)
- {
- list.Add(new RightDAL().ToModel(row));
- }
- repRightList.DataSource = list;
- repRightList.DataBind();
- }
- }
复制代码 SQLHelper中的ExecuteDataTable方法:
- public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
- {
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sql;
- cmd.Parameters.AddRange(parameters);
- DataSet ds = new DataSet();
- SqlDataAdapter adapter = new SqlDataAdapter(cmd);
- adapter.Fill(ds);
- return ds.Tables[0];
- }
- }
- }
复制代码
|