如何做联合查询,100分,晚上结贴

topfistcn 2002-07-30 09:39:08
如题,我想实现的功能是这样的:有许多查询选项,查询选项可选,然后组合到一起查询。请大家说说自己的看法。先谢了
...全文
30 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluefish922 2002-07-30
  • 打赏
  • 举报
回复
大致就同上面各位所说,我一般用SESSION来组合。
If Trim(str3) <> "" Then
If Session("findwhere") <> "where" Then
Session("findwhere") = Session("findwhere") & d31 & " shdh like '%" & str3 & "%' "
Else
Session("findwhere") = Session("findwhere") & " shdh like '%" & str3 & "%' "
End If
End If
hbxtx 2002-07-30
  • 打赏
  • 举报
回复
这是我做的一个多选项(每个chechbox后有一个textbox或Dropdownlist供输入数据)查询,你参考一下:

if((checkBox1.Checked)||(checkBox2.Checked)||(checkBox3.Checked)||(checkBox4.Checked))
{
//start query
string condition="";
if(checkBox1.Checked)
condition+="(qd.科室=\'"+DropDownList1.Text.Trim()+"\')";
if(checkBox2.Checked)
condition+="and(qd.规格=\'"+DropDownList2.Text.Trim()+"\')";
if(checkBox3.Checked)
condition+="and(qd.名称=\'"+DropDownList3.Text.Trim()+"\')";
if(checkBox4.Checked)
condition+="and(qd.机号 like \'%"+TextBox1.Text.Trim()+"%\')";

if(condition.Substring(0,3)=="and")
{
//move add
condition=condition.Substring(3,condition.Length-3);
}


string condition1="select * from qd where "+condition;

sqlCommand1.CommandText=condition1;
sqlCommand1.Connection=sqlConnection1;

System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter();

System.Data.DataSet ds=new System.Data.DataSet();

da.SelectCommand=sqlCommand1;

da.Fill(ds,"qd");

sqlConnection1.Close();

DataGrid1.DataSource=ds.Tables["qd"].DefaultView;
DataGrid1.DataBind();

...
baitianhai 2002-07-30
  • 打赏
  • 举报
回复
大概就是这样的!
MFC2001 2002-07-30
  • 打赏
  • 举报
回复
组合sql语句,生成不同的DataView,绑定到dataGrid

或者是使用DataView的RowFilter来过滤

meetweb 2002-07-30
  • 打赏
  • 举报
回复
可以使用判断语句
if(condition1)
sql1=""+condition1;
else
if(condition2)
sql1=""+condition2;
else
if(condition3)
sql1=""+condition3;
然后使用sqlcommand,次方法是使用查询组合到Sql语句中,方法简单易用


mbm 2002-07-30
  • 打赏
  • 举报
回复
两种方法
根据选择项或输入项改变sql语句访问库表查询
绑顶到dataview,通过rowfilter属性过滤数据
2的code
private void bt_Query_Click(object sender, System.EventArgs e)
{
DataGrid1.CurrentPageIndex=0;
DataView myview=((DataSet)ViewState["ds"]).Tables[0].DefaultView;
string filter="iId is not null";
if(tb_Begin.Text!="")
{
filter+=" and dDate>='"+Convert.ToDateTime(tb_Begin.Text.Trim())+"'";

}
if(tb_End.Text!="")
{
filter+=" and dDate<='"+Convert.ToDateTime(tb_End.Text.Trim())+"'";
}
if(tb_Handler.Text!="经手人"&&tb_Handler.Text!="")
{
filter+=" and cHandler like '%"+tb_Handler.Text.Trim()+"%'";
}
if(ddl_OutIn.SelectedIndex!=0)
{
filter+=" and cInOrOut='"+ddl_OutIn.SelectedItem.Text+"'";
}
if(tb_Name.Text!="物品名称"&&tb_Name.Text!="")
{
filter+=" and cName like '%"+tb_Name.Text+"%'";
}
myview.RowFilter=filter;
ViewState["filter"]=filter;
DataGrid1.DataSource=myview;
DataGrid1.DataBind();
}
insect_wsj 2002-07-30
  • 打赏
  • 举报
回复
将查询条件设成变量,通过判断语句生成变量值,然后进行查询。该函数可以考虑做到WebService核心里去,以供调用。
Richard2001 2002-07-30
  • 打赏
  • 举报
回复
If txtName.Text<>"" then
strSql=strSql & " And [Name] = '" & txtName.Text & "' "
End If
If List1.SelectedIndex<>0 then
strSql=strSql & " And [DeptID] = '" & List1.Items(List1.SelectedIndex).Value & "' "
End If

...


62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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