现在网站首页公告需要生成一条Js.实现把数据库的内容写入Js里.后台数据库挂了.网站照样可以读取到公告;网站和后台分为两台服务器.而数据库在后台服务器上.
我想问下.如何在后台操作.数据库时.让网站的Js及时生成新的Js文件呢??

解决方案 »

  1.   

    创建一个Httphandler
    在这个handler里面读取数据后, 将文件结果缓存到Cache,然后返回js文件结果。如果数据库错误且Cache不为空,就返回Cache中的结果
      

  2.   

    数据库和后台在一台服务器上.网站则在另一台服务器上.
    现在主要想如何在后台更新数据的时候网站的Js文件要及时更新.难道要实时监控数据库.有数据变化时就更新Js文件?
      

  3.   

    哪用那么麻烦呢 public void makenewshtml(string filename,string str,int id)
        {
            StringBuilder strhtml = new StringBuilder();
            using (StreamReader sr = new StreamReader(Server.MapPath("../../templates/newsshow.htm"), Encoding.GetEncoding("gb2312")))
            {
                String online;
                while ((online = sr.ReadLine()) != null)
                {
                    strhtml.Append(online);
                }
                sr.Close();
            }
            string html = "";
            string sq = "select * from n_catalog order by n_cid";
            DataTable dt = sqlhelper.GetTable(sq);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<li><a href='../news/" + dt.Rows[i]["n_cid"].ToString() + "'>" + getcname(int.Parse(dt.Rows[i]["n_cid"].ToString())) + "</a></li>";
            }
            html = html.Replace('\'', '\"');
            html = "document.write('" + html + "');";        string fpath = Server.MapPath("../../js/newclass.js");
            FileInfo fl = new FileInfo(fpath);
            using (FileStream fs = fl.Create())
            {
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
                sw.Write(html);
                sw.Flush();
                sw.Close();
            }        string scr = "<script language='javascript' type='text/javascript' src='../js/newclass.js?flag=" + DateTime.Now.ToString() + "'></script>";
            scr = scr.Replace('\'', '\"');
            strhtml = strhtml.Replace("$class$", scr);
            string st = "select k_word from k_keywords where n_nid=" + id;
            DataTable dt1 = sqlhelper.GetTable(st);//取得所浏览新闻ID的关键字
            string stc = "select distinct top 4 n.n_nid,n_title,n_time from n_news n left join k_keywords k on k.n_nid=n.n_nid where";
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                stc += " k_word like '%" + dt1.Rows[i]["k_word"].ToString() + "%' or";
            }
            stc = stc.Substring(0, stc.Length - 2);// -2是去掉 or这个字符串,拼接SQL语句
            stc = stc + "order by n_time desc";
            DataTable dtabout = sqlhelper.GetTable(stc);
            for (int i = 0; i < dtabout.Rows.Count; i++)//DataTable删除行,相关新闻需要去除本身ID
            {
                if (Convert.ToInt32(dtabout.Rows[i]["n_nid"]) == id)
                {
                    dtabout.Rows.RemoveAt(i);
                }
            }
            string htm = "";
            for (int i = 0; i < dtabout.Rows.Count; i++)
            {
                if (dtabout.Rows[i]["n_title"].ToString() == "")
                {
                    continue;
                }
                string sqc = "select statichtml from n_news where n_nid=" + int.Parse(dtabout.Rows[i]["n_nid"].ToString());
                string link = "";
                object obj = sqlhelper.ExecuteScalar(sqc);
                if (obj != null)
                {
                    link = obj.ToString();
                }
                htm += "<li>·<a href='" + link + "'>" + dtabout.Rows[i]["n_title"].ToString() + "</a></li>";
            }
            strhtml = strhtml.Replace("$n_about$", htm);        strhtml = strhtml.Replace("$n_title$", title.Value);
            strhtml = strhtml.Replace("$n_time$", DateTime.Now.ToString());
            strhtml = strhtml.Replace("$n_hit$", hit.Text);
            strhtml = strhtml.Replace("$n_content$", fck.Value);
            strhtml = strhtml.Replace("$n_cname$", getcname(int.Parse(str)));
            Regex.Replace(strhtml.ToString(), @"(<\/[^>]*?>)", "$1\n");
            string filepath = Server.MapPath("../../news/") + filename;
            FileInfo file = new FileInfo(filepath);
            using (FileStream fs = file.Create())
            {
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
                sw.WriteLine(strhtml);
                sw.Flush();
                sw.Close();
            }
        }
      

  4.   

    更新一篇新闻,就把那个JS重新写一次,(还是原来的那个JS)
      

  5.   

    你能保证运行.net代码,还用怕服务器挂了吗?
      

  6.   

    后台和数据库在一台服务器上!
    而网站前台则在另外一台服务器上!后台数据库挂了..前面的公告当然可以运行了.因为前台网站是读取Js里的内容公告.
    跟数据库没关系.只要后台添加.更新时更新一下前台的Js文件就OK了.