功能是:按照用户所选择的城市ID,分别列出此城市下属的分区以及餐馆菜系类型。。代码为:Diqu.Items.Clear();
int d = int.Parse(Chengshi.SelectedItem.Value.ToString());
Diqu.Items.Insert(0, new ListItem("选择地区", ""));
FillDropDownList(0,d);Caixi.Items.Clear();
int dc = int.Parse(Chengshi.SelectedItem.Value.ToString());
Caixi.Items.Insert(0, new ListItem("请选择菜系", ""));
FillDropDownList1(0,dc);
private void FillDropDownList(int depth, int categoryID )
{
  SqlConnection connString=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
  string strSQL = "Select * From Chengshi Where ThreadID = " + categoryID.ToString();
  SqlDataAdapter da = new SqlDataAdapter(strSQL, connString);
  DataTable dt = new DataTable();
  da.Fill(dt);
  foreach(DataRow dr in dt.Rows)
  {
int id = Convert.ToInt32(dr["CSid"]);
int parentid = Convert.ToInt32(dr["ThreadID"]);
string sep = new string(' ', depth);
sep += new string('-', depth);
ListItem li = new ListItem(sep + dr["Chengshi"].ToString(), id.ToString());
this.Diqu.Items.Add(li);
FillDropDownList(depth + 1, id);
  }
  connString.Close();
}private void FillDropDownList1(int depth, int categoryID )
{
  SqlConnection connString=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
  string strSQL = "Select * From QY_Class Where CSID = " + categoryID.ToString() + " AND Lanmu = '餐馆'";
  SqlDataAdapter da = new SqlDataAdapter(strSQL, connString);
  DataTable dt = new DataTable();
  da.Fill(dt);
  foreach(DataRow dr in dt.Rows)
  {
int id = Convert.ToInt32(dr["Cid"]);
int parentid = Convert.ToInt32(dr["ThreadID"]);
string sep = new string(' ', depth);
sep += new string('-', depth);
ListItem li = new ListItem(sep + dr["Cname"].ToString(), id.ToString());
this.Caixi.Items.Add(li);
FillDropDownList1(depth + 1, id);
  }
  connString.Close();
}现在执行的结果是,如果两条都执行的话,页面反应效率就会很慢。而且CPU高上升至100%,如果单独执行一条就正常,没问题。。求大家帮忙优化一下代码。能否合并成一条执行,或者写成类来代调。新手不懂,请大家多指点一下。

解决方案 »

  1.   

    帮你改写了第一个,第二个你试着写一下。
    private void FillDropDownList(int depth, int categoryID )
    {
       SqlConnection connString=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
       string strSQL = "Select * From Chengshi ";);
       SqlDataAdapter da = new SqlDataAdapter(strSQL, connString);
       DataTable dt = new DataTable();
       da.Fill(dt);
    FillDDLFromTable(depth,categoryID,ref dt);
       connString.Close();
    }
    private void FillDDLFromTable(int depth,int categoryID,ref DataTable dt)
    {
    dt.Select("ThreadID='"+ categoryID+"'");
    foreach(DataRow row in dt.Rows)
    {
    int id = (int)row["CSid"];
    int parentid = (int)row["ThreadID"];
    string sep = new string(' ', depth);
    sep += new string('-', depth);
    ListItem li = new ListItem(sep + dr["Chengshi"].ToString(), id.ToString());
    this.Diqu.Items.Add(li);
    FillDDLFromTable(depth + 1, id,ref dt);
    } }
      

  2.   

    再改一下,连接关闭提前。private void FillDropDownList(int depth, int categoryID )
    {
       SqlConnection connString=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
       string strSQL = "Select * From Chengshi ";);
       SqlDataAdapter da = new SqlDataAdapter(strSQL, connString);
       DataTable dt = new DataTable();
       da.Fill(dt);
    connString.Close(); FillDDLFromTable(depth,categoryID,ref dt);
      
    }
    private void FillDDLFromTable(int depth,int categoryID,ref DataTable dt)
    {
    dt.Select("ThreadID='"+ categoryID+"'");
    foreach(DataRow row in dt.Rows)
    {
    int id = (int)row["CSid"];
    int parentid = (int)row["ThreadID"];
    string sep = new string(' ', depth);
    sep += new string('-', depth);
    ListItem li = new ListItem(sep + dr["Chengshi"].ToString(), id.ToString());
    this.Diqu.Items.Add(li);
    FillDDLFromTable(depth + 1, id,ref dt);
    } }
      

  3.   

    鏈嶅姟鍣ㄥ簲鐢ㄧ▼搴忎笉鍙敤 
    鎮ㄨ瘯鍥惧湪姝?Web 鏈嶅姟鍣ㄤ笂璁块棶鐨?Web 搴旂敤绋嬪簭褰撳墠涓嶅彲鐢ㄣ€傝鐐瑰嚮 Web 娴忚鍣ㄤ腑鐨勨€滃埛鏂扳€濇寜閽噸璇曟偍鐨勮姹傘€? 绠$悊鍛樻敞鎰忎簨椤? 璇﹁堪姝ょ壒瀹氳姹傚け璐ュ師鍥犵殑閿欒淇℃伅鍙湪 Web 鏈嶅姟鍣ㄧ殑绯荤粺浜嬩欢鏃ュ織涓壘鍒般€傝妫€鏌ユ鏃ュ織椤逛互鏌ユ槑瀵艰嚧璇ラ敊璇彂鐢熺殑鍘熷洜銆? 
    我改了一下,但出现这样的情况,跟我说的问题一样,现在只执行一条都发生这样的情况了。谢谢你的帮助,但这个方法似乎不行啊。
      

  4.   

    private void FillDDLFromTable(int depth,int categoryID,ref DataTable dt)
    {
    DataRow[] rows =  dt.Select("ThreadID='"+ categoryID+"'");
    foreach(DataRow row in rows)
    {
    int id = (int)row["CSid"];
    int parentid = (int)row["ThreadID"];
    string sep = new string(' ', depth);
    sep += new string('-', depth);
    ListItem li = new ListItem(sep + row["Chengshi"].ToString(), id.ToString());
    //this.Diqu.Items.Add(li);
    FillDDLFromTable(depth + 1, id,ref dt);
    } }
      

  5.   

    DataRow[] rows =  dt.Select("ThreadID='"+ categoryID+"'");
    foreach(DataRow row in rows)这里改一下,要用查询的结果,不能用 in dt.rows.要不然白查询了,也会死循环。另外要确定 数据中没有 id = 自己的 ThreadID的记录。
      

  6.   

    主要的问题就是不要在递归中查数据库。
    asp.net 提供了一个很好的东西那就是 内存表。DataTable 是一个相当好用的东西。先把要显示的数据一次全查出来,然后去到它里面查询。先查第一级,在显示第一级的同时去显示它的下级。
      

  7.   

    我检查了好多遍,但两个一起运行就会报错,出乱码。占用CPU100%
      

  8.   

    已经被优化的代码似乎没问题,有个问题是:递归是否无法终止?如ThreadID和CSid会不会有值相同的情况?那样的话CPU100%,并抱错就会发生了。建议先检查下数据:)
      

  9.   

    问题解决了,原来第二个递归所查询的数据表跟第一个有区别,我没有把这里更改掉。现在多加一个参数,问题解决。。谢谢cpp2017马上结帐给分。
      

  10.   

    cool 数据库要被你用滥掉了^_^
      

  11.   

    难道又是一个点评网?------------------------
    www.cndianping.com
    中国点评网。
      

  12.   

    Select * From Chengshi;Select * From QY_Class
    检查一下表Chengshi和QY_Class有大容量字段.
    一般建议不要用"*" 而是选择该选的字段.