这样代码怎么优化?
private void Page_Load(object sender, System.EventArgs e)
{
//显示畅销书籍
SqlConnection conn1 = new SqlConnection(strHuntBook);
conn1.Open();
string str2 = "select top 3 * from APD order by id desc";
SqlDataAdapter thisAdapter1 = new SqlDataAdapter(str2,conn1);
DataSet thisDataSet1 = new DataSet();
thisAdapter1.Fill(thisDataSet1,"lyb");
Datalist_changxiaoshu.DataSource = thisDataSet1.Tables["lyb"].DefaultView;
Datalist_changxiaoshu.DataBind();
conn1.Close(); //---------------------------------------
//显示热门点击书籍
SqlConnection conn11 = new SqlConnection(strHuntBook);
conn11.Open();
string str22 = "select top 3 * from APD order by id desc";
SqlDataAdapter thisAdapter11 = new SqlDataAdapter(str22,conn11);
DataSet thisDataSet11 = new DataSet();
thisAdapter11.Fill(thisDataSet11,"lyb");
Datalist_Hot.DataSource = thisDataSet11.Tables["lyb"].DefaultView;
Datalist_Hot.DataBind();
conn11.Close(); //--------------------------------------
//显示热门评论
SqlConnection conHotForum = new SqlConnection(strForum);
conHotForum.Open();
string stringHotForum = "select top 20 * from questions where BigClass_ID = '100' order by HiteCounter desc";
SqlDataAdapter AdapterHotForum = new SqlDataAdapter(stringHotForum,conHotForum);
DataSet DataSetHotForum = new DataSet();
AdapterHotForum.Fill(DataSetHotForum,"lyb");
Datalist_HotForum.DataSource = DataSetHotForum.Tables["lyb"].DefaultView;
Datalist_HotForum.DataBind();
conHotForum.Close();
}
我感觉这样写虽然可以运行,但是否效率比较低,求高手优化

解决方案 »

  1.   

    放到
    if (!IsPostBack)
    {
    ....
    }
      

  2.   

    我通常也这样写的...
    不过我觉得可以写一个函数
    public DataSet GetDataSet(string strCommandString,string strTableName)
    {
       ......
       return DataSet
    }
    这样你每次只要传入查询语句和表名就可以直接调用了
    省很多重复的代码
      

  3.   

    提供两个意见仅供参考。
    (1)把所有的string类型变量用stringBuilder代替。
    (2)DataSet和页面缓存。