ASP.net 多条件查询

lovelilei2045 2008-02-29 12:40:05
各位大侠,小弟做个网站!用asp.net 要实现多条件查询!
checkbox1 text1
checkbox2 text2
xheckbox3 text3
buttonOK
gridview 输出显示

主要是想这样实现 str sql = "select * from tb_student where 1=1";
if(checkbox1.checked){sql = sql + "and StuName like '%"+text1.Text+"%' ";}
if(checkbox2.checked) {sql = sql + "and StuID like '%"+text2.Text+"%' ";}
if(checkbox3.checked) {sql = sql + "and StuADD like '%"+text3.Text+"%' ";}
sqldatasource1.selectcammand = sql;

主要是用vs2005 的控件做 语言是C#,那位大哥有完整的代码共享下,谢谢啊!
...全文
1259 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
猫十三 2010-01-19
  • 打赏
  • 举报
回复
正好过来看看,关于这个多条件查询,我理解lz的想法,因为多条件查询的同时也意味着,无条件的全部查询,所以必须考虑好如何实现条件的插入等
xray2005 2008-02-29
  • 打赏
  • 举报
回复

str sql = "select * from tb_student where 1=1";
if(checkbox1.checked)
sql += " and StuName like '%"+text1.Text+"%' ";
if(checkbox2.checked)
sql += " and StuID like '%"+text2.Text+"%' ";
if(checkbox3.checked)
sql += " and StuADD like '%"+text3.Text+"%' ";
sqldatasource1.selectcammand = sql;

blestcc 2008-02-29
  • 打赏
  • 举报
回复
你写的很清楚了啊,就这样组合就行了
菜鸟A队 2008-02-29
  • 打赏
  • 举报
回复
楼主你先把CheckBox的autopostback属性设成true就好事了
TNT_1st_excellence 2008-02-29
  • 打赏
  • 举报
回复
你是不是想這樣啊?
-----------------
1.進入頁面后,選擇CheckBox
2.點擊buttonOK
3.顯示 gridview 資料
---------------

 private void GetSqlString()
{
string sql = @"SELECT * FROM tb_Server WHERE 1=1 ";
if(CheckBox1.Checked)
sql += @" and StuName like '%"+text1.Text+"%' ";
if(CheckBox2.Checked)
sql += @" and StuID like '%"+text2.Text+"%' ";
if(CheckBox3.Checked)
sql += @" and StuADD like '%"+text3.Text+"%' ";

SqlData1.SelectCommand = GetSqlString();
}

lovelilei2045 2008-02-29
  • 打赏
  • 举报
回复
即使这样把checkbox屏蔽了,将sql语句分开写也不可以,这样的话就将所有结果都显示出来,而并没有筛选!
String sql = "SELECT * FROM tb_Server WHERE 1=1 ";
if (!IsPostBack)
{
//if (CheckBox2.Checked)
{
sql = sql + "and (ServerIP = @paramIP)";
ControlParameter SelIP = new ControlParameter("paramIP", "txtIP", "text");
SqlData1.SelectParameters.Add(SelIP);
}
// if (CheckBox2.Checked)
{
sql = sql + "and(CusID = @paramCus)";
ControlParameter selCus = new ControlParameter("paramCus", "txtCus", "Text");
SqlData1.SelectParameters.Add(selCus);
}
}
SqlData1.SelectCommand = sql;
绝代坏坏 2008-02-29
  • 打赏
  • 举报
回复
2楼正解
lovelilei2045 2008-02-29
  • 打赏
  • 举报
回复
SqlData1.ConnectionString = WebConfigurationManager.ConnectionStrings["SZIDCConnectionString"].ConnectionString;
String sql = "SELECT * FROM tb_Server WHERE 1=1 and (ServerIP = @paramIP)and(CusID = @paramCus) ";
if (!IsPostBack)
{
ControlParameter SelIP = new ControlParameter("paramIP", "txtIP", "text");
SqlData1.SelectParameters.Add(SelIP);


ControlParameter selCus = new ControlParameter("paramCus", "txtCus", "Text");
SqlData1.SelectParameters.Add(selCus);
}
SqlData1.SelectCommand = sql;
这样的话是可以查询的,但是必须要输入2个查询条件,如果只输入一个的话就查询不了
所以我想加个判断 String sql = "SELECT * FROM tb_Server WHERE 1=1"
if (!IsPostBack)
{
if (CheckBox1.Checked)
{
ControlParameter SelIP = new ControlParameter("paramIP", "txtIP", "text");
SqlData1.SelectParameters.Add(SelIP);
sql = sql + "and (ServerIP = @paramIP)";
}
if(checkbox2.checked){..;)
但是这样不可以
kzc_ljl 2008-02-29
  • 打赏
  • 举报
回复
如果你查不出来的话,可能是符号有问题

SELECT
*
FROM
tb_student
WHERE
1=1 AND StuName LIKE '%' + @charname + '%'

存储过程是这样写的

我想是不是应该写成
"and StuName like '%' " + text1.Text + " '%' "

试试看,注意符号。一个一个的测试下
lovelilei2045 2008-02-29
  • 打赏
  • 举报
回复
回1楼的,主要是checkbox 是否选取不会用 就是 if(checkbox1.checked){...} 当选取后,并不执行后面的语句!
public partial class selectCUP : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlData1.ConnectionString = WebConfigurationManager.ConnectionStrings["SZIDCConnectionString"].ConnectionString;
String sql = "select * from tb_server where 1=1";

if (CheckBox1.Checked)
{ sql = sql + "and ServerIP like '%" + txtIP.Text + "%' "; }
SqlData1.SelectCommand = sql;
}

protected void btn1_Click(object sender, EventArgs e)
{

}
}

是否语句放的函数地方不对?
zhangjjhua 2008-02-29
  • 打赏
  • 举报
回复
自己都写出来了,叫我们怎么写啊,,, 楼上的可以了,,

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧