关于数据库内容生成Js的问题 现在网站首页公告需要生成一条Js.实现把数据库的内容写入Js里.后台数据库挂了.网站照样可以读取到公告;网站和后台分为两台服务器.而数据库在后台服务器上.我想问下.如何在后台操作.数据库时.让网站的Js及时生成新的Js文件呢?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 创建一个Httphandler在这个handler里面读取数据后, 将文件结果缓存到Cache,然后返回js文件结果。如果数据库错误且Cache不为空,就返回Cache中的结果 数据库和后台在一台服务器上.网站则在另一台服务器上.现在主要想如何在后台更新数据的时候网站的Js文件要及时更新.难道要实时监控数据库.有数据变化时就更新Js文件? 哪用那么麻烦呢 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(); } } 更新一篇新闻,就把那个JS重新写一次,(还是原来的那个JS) 你能保证运行.net代码,还用怕服务器挂了吗? 后台和数据库在一台服务器上!而网站前台则在另外一台服务器上!后台数据库挂了..前面的公告当然可以运行了.因为前台网站是读取Js里的内容公告.跟数据库没关系.只要后台添加.更新时更新一下前台的Js文件就OK了. 高手进!小弟跪求! 关于IIS的问题,请大家给解决一下 一个域名+空间,可以不可以挂两个.net 网站呢?两个bin文件夹 程序运行的走向? 关于发布的问题 100分求在.net下做新闻采集的技术! 动态添加tab页和关闭tab页 请问如何在button1中调用button2得程序代码? 可否实现如下这种功能 突然看到的一个问题,位移和乘法哪个速度快些? 网站遭受攻击 不知各位遇到过没有,请各位高手帮分析下。 帮忙看下怎么才能执行代码
在这个handler里面读取数据后, 将文件结果缓存到Cache,然后返回js文件结果。如果数据库错误且Cache不为空,就返回Cache中的结果
现在主要想如何在后台更新数据的时候网站的Js文件要及时更新.难道要实时监控数据库.有数据变化时就更新Js文件?
{
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();
}
}
而网站前台则在另外一台服务器上!后台数据库挂了..前面的公告当然可以运行了.因为前台网站是读取Js里的内容公告.
跟数据库没关系.只要后台添加.更新时更新一下前台的Js文件就OK了.