有两个for循环语句,里面都对数据库查询,速度很慢,如何优化?

fxs320 2010-05-20 10:33:36
如题,


if (ds.Tables.Count > 0)
{
int cpidcount;
int cpcount;
for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
{

if (int.TryParse(ds.Tables[0].Rows[m][1].ToString(),out cpcount))
{
if (cpcount > 1)
{
for (int j = 0; j < cpcount; j++)
{
TableRow tbcpsec = new TableRow();
TableCell tbcpidsec = new TableCell();
TableCell tbcpvlsec = new TableCell();
cpidcount = j + 1;
tbcpidsec.Text = ds.Tables[0].Rows[m][0].ToString() + cpidcount + ":";
tbcpvlsec.Text = Production.GetCompentIDBySNAndPD(txtSN.Text, ds.Tables[0].Rows[m][0].ToString() ,j);
tbcpsec.Cells.Add(tbcpidsec);
tbcpsec.Cells.Add(tbcpvlsec);
Table1.Rows.Add(tbcpsec);
cpID.Add(ds.Tables[0].Rows[m][0].ToString() + cpidcount);
cpValue.Add (Production.GetCompentIDBySNAndPD(txtSN.Text, ds.Tables[0].Rows[m][0].ToString(), j));
}

}
else
{
TableRow tr = new TableRow();
TableCell tcID = new TableCell();
TableCell tcIDVl = new TableCell();
tr.Cells.Add(tcID);
tr.Cells.Add(tcIDVl);
tcID.Text = ds.Tables[0].Rows[m][0].ToString() + ":";
tcIDVl.Text = Production.GetCompentIDBySN(txtSN.Text, ds.Tables[0].Rows[m][0].ToString());
Table1.Rows.Add(tr);
cpID.Add(ds.Tables[0].Rows[m][0].ToString());
cpValue.Add(Production.GetCompentIDBySN(txtSN.Text, ds.Tables[0].Rows[m][0].ToString()));
}

}


}
...全文
1175 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Teng_s2000 2010-05-20
  • 打赏
  • 举报
回复
1.使用SqlDataReader读取数据,然后绑定
2.对数据进行分页操作,在优化,如果你一下将100W的纪录一起显示到web上也会超时
moonwrite 2010-05-20
  • 打赏
  • 举报
回复
Production.GetCompentIDBySN(txtSN.Text, ds.Tables[0].Rows[m][0].ToString());
cpValue.Add(Production.GetCompentIDBySN(txtSN.Text, ds.Tables[0].Rows[m][0].ToString()));

想比这两个总是Open()+Close() 这是很耗
可以尝试把Open和Close()放在外面 反正就是尽量减少Open()+Close() 的次数

Ray_Yang 2010-05-20
  • 打赏
  • 举报
回复
加内存 升级CPU
段传涛 2010-05-20
  • 打赏
  • 举报
回复
可以试试 foreach
mngzilin 2010-05-20
  • 打赏
  • 举报
回复
用foreach代替for
zzxap 2010-05-20
  • 打赏
  • 举报
回复
 

if (ds.Tables.Count > 0)
{int m = 0;
int cpidcount;
int cpcount;
int a=ds.Tables[0].Rows.Count;


for ( m = 0; m <a; m++)
{

if (int.TryParse(ds.Tables[0].Rows[m][1].ToString(),out cpcount))
{
if (cpcount > 1)
{
for (int j = 0; j < cpcount; j++)
{TableRow tbcpsec = new TableRow();
TableCell tbcpidsec = new TableCell();
TableCell tbcpvlsec = new TableCell();
cpidcount = j + 1;
tbcpidsec.Text = ds.Tables[0].Rows[m][0].ToString() + cpidcount + ":";
tbcpvlsec.Text = Production.GetCompentIDBySNAndPD(txtSN.Text, ds.Tables[0].Rows[m][0].ToString() ,j);
tbcpsec.Cells.Add(tbcpidsec);
tbcpsec.Cells.Add(tbcpvlsec);
Table1.Rows.Add(tbcpsec);
cpID.Add(ds.Tables[0].Rows[m][0].ToString() + cpidcount);
cpValue.Add (Production.GetCompentIDBySNAndPD(txtSN.Text, ds.Tables[0].Rows[m][0].ToString(), j));
}

}
else
{
TableRow tr = new TableRow();
TableCell tcID = new TableCell();
TableCell tcIDVl = new TableCell();
tr.Cells.Add(tcID);
tr.Cells.Add(tcIDVl);
tcID.Text = ds.Tables[0].Rows[m][0].ToString() + ":";
tcIDVl.Text = Production.GetCompentIDBySN(txtSN.Text, ds.Tables[0].Rows[m][0].ToString());
Table1.Rows.Add(tr);
cpID.Add(ds.Tables[0].Rows[m][0].ToString());
cpValue.Add(Production.GetCompentIDBySN(txtSN.Text, ds.Tables[0].Rows[m][0].ToString()));
}

}


}

尽量不要再for循环内定义变量
sxiaohui8709253 2010-05-20
  • 打赏
  • 举报
回复
可能需要转变思路
jianshao810 2010-05-20
  • 打赏
  • 举报
回复
如果优化的话就从查询下功夫吧 进而减少一个循环 但不知你的业务需求是否可行
烈火晴天 2010-05-20
  • 打赏
  • 举报
回复
是你机器慢吧!
cuike519 2010-05-20
  • 打赏
  • 举报
回复
根本的方法是优化设计方案吧或者算法。

如果只是修改现在的代码,数据库部分你应该缓存到哈希表中,如果量太大还是考虑第一种方法吧。
ckl881003 2010-05-20
  • 打赏
  • 举报
回复
把数据一次查出来。。别查这么多次。。
Justin-Liu 2010-05-20
  • 打赏
  • 举报
回复
如果优化的话就从查询下功夫吧 进而减少一个循环 但不知你的业务需求是否可行
李班头 2010-05-20
  • 打赏
  • 举报
回复
很难优化。
demoooo 2010-05-20
  • 打赏
  • 举报
回复
不知道业务情况

如果数据是关联的,可以一次性查询出来,在数据集中for, 不要for着去查询数据.一旦cpcount很大,速度必定就很慢
antiking 2010-05-20
  • 打赏
  • 举报
回复
查询显示用用sqldatareader
丰云 2010-05-20
  • 打赏
  • 举报
回复
依照你的代码体现的业务来看,

没有办法优化。。。。

62,243

社区成员

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

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

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

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