private void ShowData()
{
Sex = getSex();
CityCode = getCityCode();
            Conn = new SqlConnection(ConnStr);
try
{
if(this.radioButton1.Checked)
{
SqlDataAdapter adapter1 = new SqlDataAdapter();
adapter1.TableMappings.Add("Table","TVR_IDData");
Conn.Open();
if(CityCode=="")
{
sql = "select userid as ID,TelNumber as 手机号码,usex as 性别,uage as 年龄,";
sql += "convert(varchar,idenableddatetime,120) as 注册时间,onlineenabled as 是否在线,";
sql += "0 拨打次数,0 费用 from tvr_iddata where curstate=1 and usex="+Sex;
}
else
{
sql = "select userid as ID,TelNumber as 手机号码,usex as 性别,uage as 年龄,";
sql += "convert(varchar,idenableddatetime,120) as 注册时间,onlineenabled as 是否在线,";
sql += "0 拨打次数,0 费用 from tvr_iddata where curstate=1 and usex="+Sex+" and ";
sql += "(left(telnumber,7) in (select mobilecode from gr_mast_mobilearea where cityno="+CityCode+"))";
}
SqlCommand comm1 = new SqlCommand(sql,Conn);
comm1.CommandType = CommandType.Text;
adapter1.SelectCommand = comm1;
                    ds = new DataSet();
adapter1.Fill(ds);
foreach(DataRow dr in ds.Tables["tvr_iddata"].Rows) 
{
string mobile = dr["手机号码"].ToString();
int fee = 0;
SqlDataAdapter adapter2 = new SqlDataAdapter();
adapter2.TableMappings.Add("Table","CTI_InBoundDetail");
    sql = "select count(*) from CTI_InBoundDetail where Ani='"+mobile+"' and Dnis='"+this.radioButton1.Text+"'";
SqlCommand comm2 = new SqlCommand(sql,Conn);
comm2.CommandType = CommandType.Text;
adapter2.SelectCommand = comm2;
DataSet ds1 = new DataSet();
adapter2.Fill(ds1);
dr["拨打次数"] = ds1.Tables["CTI_InBoundDetail"].Rows[0][0]; SqlDataAdapter adapter3 = new SqlDataAdapter();
adapter3.TableMappings.Add("Table","Bill_Detail");
sql = "select Totalbill from Bill_Detail where Telnumber='"+mobile+"' and Talklen<>0";
SqlCommand comm3 = new SqlCommand(sql,Conn);
comm3.CommandType = CommandType.Text;
adapter3.SelectCommand = comm3;
DataSet ds2 = new DataSet();
adapter3.Fill(ds2);
foreach(DataRow dr1 in ds2.Tables["Bill_Detail"].Rows)
{
fee += Int32.Parse(dr1["Totalbill"].ToString());
}
dr["费用"] = fee;
}
//MessageBox.Show(sql);
}
if(this.radioButton2.Checked)
{
MessageBox.Show(this.radioButton2.Text);
}
this.dataGrid1.DataSource = ds.Tables["tvr_iddata"].DefaultView;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Conn.Close();
}
上面的程序处理大量数据[取一条两条也很慢]时我的程序就未响应了,请各位大虾帮我改改呀,急呀。

解决方案 »

  1.   

    in (select mobilecode from gr_mast_mobilearea where cityno="+CityCode+"))";
    用in的语句先优化。in的效率太低,想办法用其他关联的方式来实现。另外自己添加一些时间的判断来查看下哪句语句耗时最长。是.net的语句还是数据库执行的语句?
    同时建议,不要在循环里面做读写数据库操作,效率太低,如果可能的话,一次性读出,然后再做关联处理。
      

  2.   

    对啊。实在不行你把几个表都先读出来。然后用dataview做关联查询也能快很多呀。
      

  3.   

    A表
    ID,用户ID,手机号,性别,年龄,注册时间
    1,12345,136000,女,23,2005-10-10B表(用户每次打入都会生成一条)
    ID,手机号,费用
    1,13600,10C表(用户只要打入了也会生成一条)
    ID,手机号
    1,136000我现在要求在DATAGRID中显示:
    用户编号,手机号,性别,年龄,注册时间,打入次数,费用大家教如何做?
      

  4.   

    如果sql语句比较复杂,不如写成存储过程,这样执行起来会快一些
      

  5.   

    A表
    ID,用户ID,手机号,性别,年龄,注册时间
    1,12345,136000,女,23,2005-10-10B表(用户每次打入都会生成一条)
    ID,手机号,费用
    1,13600,10C表(用户只要打入了也会生成一条)
    ID,手机号
    1,136000我现在要求在DATAGRID中显示:
    用户编号,手机号,性别,年龄,注册时间,打入次数,费用这就是我的需求,现在问题是,我按我自己的思路实现的代码,运行太慢,还导至软件死掉。
      

  6.   

    你用存贮过程传进去一个用户ID,A表中的字段是全部要显示出来的,只要根据b表中的用户ID进行相同的用户ID去次数就可以的阿。c表其实是用不到的,我是这么认为的
      

  7.   

    用B表建一个视图
    SELECT telno, COUNT(*) AS times, SUM(fee) AS fee
    FROM dbo.tel_zl
    GROUP BY telno
    在用A表与视图做关联查询
      

  8.   

    同意editengineerselect A.* from 
    A,
    (SELECT telno/UserID, COUNT(*) AS times, SUM(fee) AS fee
    FROM B
    GROUP BY telno) BB
    where A.telno/UserID=BB.telno/UserID