+ -
当前位置:首页 → 问答吧 → 下拉菜单和textbox联合查询数据问题

下拉菜单和textbox联合查询数据问题

时间:2011-12-21

来源:互联网

下拉菜单为数据表的列明,textbox为输入的需要查询的值,比如下拉里面有a,b,(a,b为数据表列明),在textbox里面输入
a1(a1为a列的内容)然后实现查询
我这个代码那里不对,谢谢,我这个下拉和textbox都为空白时候才能查询出来,有数据就查不出来了
SqlConnection conn;
  SqlCommand cmd;


  string cmdString1 = @"Select [日期],[教师],[异常情况],[异常事件],[请假],[请假时间],[加班],[加班小时数],[提交者] from
Teacher where ('" + this.DropDownList1.SelectedValue + "' like '" + TextBox1.Text + "') AND ('" + this.DropDownList2.SelectedValue + "' like '" + TextBox2.Text + "') AND ('" + this.DropDownList3.SelectedValue + "' like '" + TextBox3.Text + "')";
   
   
  conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=schoolhoom;Integrated Security=True;Pooling=False");
  cmd = new SqlCommand(cmdString1 , conn);
   
  conn.Open();
  SqlDataAdapter da = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  da.Fill(ds);
  GridView1.DataSource = ds;
  GridView1.DataBind();

作者: netstudy0105   发布时间: 2011-12-21

加断点调试下,看有数据的时候你的cmdString1 是什么值

作者: ycz815   发布时间: 2011-12-21

你用的是 Like 操作符,却不加 %,那还不如直接用等号,DropDownList.SelectedValue 作为字段名,却加了单引号。另外,请对 TextBox.Text 加上 Replace("'", "''"),否则很容易被人注入。
DropDownList 控件在启用 enableEventValidation="true" 的时候倒不用担心,如果未启用,你还得验证 DropDownList.SelectedValue 的合法性,否则一样注入。

作者: orain   发布时间: 2011-12-21

麻烦2楼给写个正确的句子,我知道用参数化,这样是为了先实现功能,在参数化

作者: netstudy0105   发布时间: 2011-12-21

我觉得像这样的sql语句用存储过程实现特别省事。传几个参数进去了哦了。以后维护起来要改点东西 也不用改dll文件

作者: liuleiyu121520   发布时间: 2011-12-21

该回复于2011-12-21 13:44:11被管理员删除

  • 对我有用[0]
  • 丢个板砖[0]
  • 引用
  • 举报
  • 管理
  • TOP
#6楼 得分:0回复于:2011-12-21 13:54:07
字段名不能用''引起来啊!
C# code
where ('" + this.DropDownList1.SelectedValue + "' like '" + TextBox1.Text + "') AND ('" + this.DropDownList2.SelectedValue + "' like '" + TextBox2.Text + "') AND ('" + this.DropDownList3.SelectedValue + "' like '" + TextBox3.Text + "')";
//改成:
where (" + this.DropDownList1.SelectedValue + " like '" + TextBox1.Text + "') 
AND (" + this.DropDownList2.SelectedValue + " like '" + TextBox2.Text + "') AND 
(" + this.DropDownList3.SelectedValue + " like '" + TextBox3.Text + "')";

作者: netstudy0105   发布时间: 2011-12-21

你现在的sql 实在上写成这样了:
SQL code
select* from ESApp..sc_dt_workStageRec where ('' like '') 
AND ('' like '') AND ('' like '')


所以只有你什么都不输入的时候才有数据

作者: mengmo376   发布时间: 2011-12-21

引用 6 楼 mengmo376 的回复:

字段名不能用''引起来啊!
C# code
where ('" + this.DropDownList1.SelectedValue + "' like '" + TextBox1.Text + "') AND ('" + this.DropDownList2.SelectedValue + "' like '" + TextBox2.Text + "') AND ('" + this.Dr……
+1

作者: mengmo376   发布时间: 2011-12-21

关键字 'like' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: 关键字 'like' 附近有语法错误。

源错误: 


行 33: SqlDataAdapter da = new SqlDataAdapter(cmd);
行 34: DataSet ds = new DataSet();
行 35: da.Fill(ds);
行 36: GridView1.DataSource = ds;
行 37: GridView1.DataBind();
 
代码:
 string cmdString1 = @"Select * from Teacher where (" + this.DropDownList1.SelectedValue + " like '" + TextBox1.Text + "')AND(" + this.DropDownList2.SelectedValue + " like '" + TextBox2.Text + "') AND (" + this.DropDownList3.SelectedValue + " like '" + TextBox3.Text + "')";

作者: yang_ying1983   发布时间: 2011-12-21

相关阅读 更多

热门下载

更多