protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/myinfor.htm");
        System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
        if (File.Exists(path))
        {
            StreamReader sread = new StreamReader(path, System.Text.Encoding.UTF8);
            string readfile = sread.ReadToEnd();
            sread.Close();
            SqlConnection scon = new SqlConnection("Data Source=.;Initial Catalog=Eduask_Power;User ID=sa;Persist Security Info=false");
            SqlCommand scom = new SqlCommand("select * from Ep_Student", scon);
            SqlDataAdapter sd = new SqlDataAdapter();           
            sd.SelectCommand = scom;
            DataSet ds = new DataSet();
            sd.Fill(ds, "students");
            //int i;            string path1 = Server.MapPath("~/makichen/");
            StreamWriter swrite = null;
            //string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
            //for (i = 0; i <ds.Tables["students"].Rows.Count; i++)
            //{
            foreach (DataRow i in ds.Tables["students"].Rows)
            {                string htmlfilename = "showuser" + i[1] + ".htm";
                readfile = readfile.Replace("{@userid}", i[0].ToString());
                readfile = readfile.Replace("{@userteach}", i[4].ToString());
                readfile = readfile.Replace("{@userprash}", i[5].ToString());
                 swrite = new StreamWriter(path1 + htmlfilename, false, code);
                swrite.Write(readfile);
                swrite.Close();
            }
            //}
   
        }
    }
在foreach循环那里怎么循环生成静态页面,请各位高手指教?谢谢大家

解决方案 »

  1.   

    你第一次把readFile变量中所有的{@userid}、{@userteach}、{@userprash}都替换掉了。以后再执行replace肯定不会成功。所以所有的文件的内容都是一样的。
    这样改下:
    foreach (DataRow i in ds.Tables["students"].Rows) 
                {                 string htmlfilename = "showuser" + i[1] + ".htm"; 
     
                    String content= readfile.Replace("{@userid}", i[0].ToString()); 
                    content = readfile.Replace("{@userteach}", i[4].ToString()); 
                    content = readfile.Replace("{@userprash}", i[5].ToString()); 
                    swrite = new StreamWriter(path1 + htmlfilename, false, code); 
                    swrite.Write(readfile); 
                    swrite.Close(); 
                } 
      

  2.   

    就是用一个中间变量保存replace后的结果,这样就可以避免把readfile的内容替换掉了
      

  3.   

    噢,对,楼上说的对:swrite.Write(content);