功能是:按照用户所选择的城市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%,如果单独执行一条就正常,没问题。。求大家帮忙优化一下代码。能否合并成一条执行,或者写成类来代调。新手不懂,请大家多指点一下。
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%,如果单独执行一条就正常,没问题。。求大家帮忙优化一下代码。能否合并成一条执行,或者写成类来代调。新手不懂,请大家多指点一下。
解决方案 »
- treeview用什么作数据源比较好?xml,sitemap,直接从数据库读取
- asp.net编码到底是怎么回事啊
- 急人!!怎么访问模板列中的控件?
- 菜鸟问题请教?WebService
- VS里如何操作使每一个文件夹都是一个项目呢?
- 如何动态增加模版列,并动态的在模版列中添加控件,并绑定值(C#),请高手给出完整实现代码,谢谢.
- 微软开发样例
- 初学asp.net,用户界面的问题着急请教各位大侠!
- C# jquery 加载后台数据 问题
- 最近有人申请万网asp.net的主机吗?
- 求ASPNETAtlas.vsi安装文件~~~谢谢~~~
- 请高人指点~如何才能访问 DetailsView 中 TemplateField 里定义的控件?
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);
} }
{
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);
} }
鎮ㄨ瘯鍥惧湪姝?Web 鏈嶅姟鍣ㄤ笂璁块棶鐨?Web 搴旂敤绋嬪簭褰撳墠涓嶅彲鐢ㄣ€傝鐐瑰嚮 Web 娴忚鍣ㄤ腑鐨勨€滃埛鏂扳€濇寜閽噸璇曟偍鐨勮姹傘€? 绠$悊鍛樻敞鎰忎簨椤? 璇﹁堪姝ょ壒瀹氳姹傚け璐ュ師鍥犵殑閿欒淇℃伅鍙湪 Web 鏈嶅姟鍣ㄧ殑绯荤粺浜嬩欢鏃ュ織涓壘鍒般€傝妫€鏌ユ鏃ュ織椤逛互鏌ユ槑瀵艰嚧璇ラ敊璇彂鐢熺殑鍘熷洜銆?
我改了一下,但出现这样的情况,跟我说的问题一样,现在只执行一条都发生这样的情况了。谢谢你的帮助,但这个方法似乎不行啊。
{
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);
} }
foreach(DataRow row in rows)这里改一下,要用查询的结果,不能用 in dt.rows.要不然白查询了,也会死循环。另外要确定 数据中没有 id = 自己的 ThreadID的记录。
asp.net 提供了一个很好的东西那就是 内存表。DataTable 是一个相当好用的东西。先把要显示的数据一次全查出来,然后去到它里面查询。先查第一级,在显示第一级的同时去显示它的下级。
www.cndianping.com
中国点评网。
检查一下表Chengshi和QY_Class有大容量字段.
一般建议不要用"*" 而是选择该选的字段.