请问如何循环查找出所有符合条件的数据

qian21h 2009-03-27 10:52:20
#region 权限检查
if (Session["UserInfo"] == null)
return;
LTP.Accounts.Bus.User currentUser = (LTP.Accounts.Bus.User)Session["UserInfo"];
int userid = int.Parse(currentUser.UserID.ToString());
string strWheregateid = "charindex(ltrim(wsnc_gateway.gatewayid),accounts_users.departmentid)>0 and accounts_users.UserID='" + userid + "'";
DataSet dsid = new DataSet();
dsid = gatewayidbll.GetgatewayidList(strWheregateid);
DataTable tableid = dsid.Tables[0];
//不符合条件的计数器
//int jishu = 0;
if (tableid.Rows.Count > 0)
{
//这里循环符合条件的数据,会有几个符合条件的
//我现在这样做循环到最后只显示最后一个符合条件的数据,比如我符合条件的gatewayid有 1,2,3
//但是只显示gatewayid=3的数据,怎么才能显示gatewayid所有符合的数据呢
for (int gateid = tableid.Rows.Count - 1; gateid >= 0; gateid--)
{
//int.Parse(tableid.Rows[gateid][0].ToString())是符合条件的数据库字段的数据
strWhere = "WSNC_Gateway.gatewayid='" + int.Parse(tableid.Rows[gateid][0].ToString()) + "' ";

if (Session["strWhereNodegateid"] != null && Session["strWhereNodegateid"].ToString() != "")
{
strWhere += Session["strWhereNodegateid"].ToString();
}

}
}
#endregion
DataSet ds = new DataSet();
ds = bll.GetList(strWhere);
DataView dv = ds.Tables[0].DefaultView;
gridView.DataSource = dv;
gridView.DataBind();
...全文
360 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qian21h 2009-03-27
  • 打赏
  • 举报
回复
调试成功了,感谢各位
cpp2017 2009-03-27
  • 打赏
  • 举报
回复
用in

string strWhere ="";
for(.........
{
strWhere += "'"+ 值+"',";
}
if(strWhere.Length>0)
{
strWhere = strWhere.SubString(0,strWhere.Length-1);
sql += " WHERE aid in("+strWhere +")"
}
qian21h 2009-03-27
  • 打赏
  • 举报
回复
hecong875
谢谢,看起来你的方法应该可行,调试中
qian21h 2009-03-27
  • 打赏
  • 举报
回复
额,估计我代码截的太多了,其实是这样的
我有个a表
a表中有字段 aid
然后我有个条件查出符合特定条件的aid有1,2,3
我怎么把这个where语句循环的查找出来
其实放sql里面查找很简单

select aid from a where aid='1' or aid='2' or aid='3'

怎么把后面的where条件放在程序里面 查找出来呢

放在程序里面查找那是因为符合条件的aid是变化的
hecong875 2009-03-27
  • 打赏
  • 举报
回复


if (tableid.Rows.Count > 0)
{

string Strid=string.Empty;
for (int i = 0;tableid.Rows.Count >= i; i++)
{
//int.Parse(tableid.Rows[gateid][0].ToString())
Strid +="'"+ int.Parse(tableid.Rows[i][0].ToString()+"',";
// strWhere = "WSNC_Gateway.gatewayid='" + int.Parse(tableid.Rows[gateid][0].ToString()) + "' ";

if (!String.IsNullOrEmpty(Session["strWhereNodegateid"]))
{
strWhere += Session["strWhereNodegateid"].ToString();
}

}
string strWhere = "WSNC_Gateway.gatewayid in("+Strid.Substring(0,Strid.Length-1)+")";

具体没测试! 大概就是这样字的
CutBug 2009-03-27
  • 打赏
  • 举报
回复
if (Session["strWhereNodegateid"] != null && Session["strWhereNodegateid"].ToString() != "")
{
strWhere += Session["strWhereNodegateid"].ToString();
}
这个放在for循环里做什么吗?
liuyeede 2009-03-27
  • 打赏
  • 举报
回复
使用过滤表达式一次将满足条件的记录都选出来,不需要使用循环。
CutBug 2009-03-27
  • 打赏
  • 举报
回复
string[] gateSet = new string[tableid.Rows.Count - 1];
strWhere = "WSNC_Gateway.gatewayid in("
for (int gateid = tableid.Rows.Count - 1; gateid >= 0; gateid--)
{
gateSet[i]= tableid.Rows[gateid][0].ToString();
}
strWhere +=String.Join(",",gateSet)+")";




cpp2017 2009-03-27
  • 打赏
  • 举报
回复
没看懂楼主的意思

strWhere =dt的rows的字段,

然后又+上了session中的id,得出的结果可能就是WSNC_Gateway.gatewayid='1'2345 这样不会出错么?

62,268

社区成员

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

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

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

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