⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controldatabase.cs

📁 网上自动答疑系统(C#)
💻 CS
📖 第 1 页 / 共 4 页
字号:
					int renameNum=0;
					while(fileExist)
					{
						fileName=keyword+renameNum;
						renameNum++;
						fileExist=File.Exists(filePath+"\\"+fileName+".txt");
					}
					StreamWriter sw=File.CreateText(filePath+"\\"+fileName+".txt");
					sw.WriteLine("您搜索有关\""+keyword+"\"的记录,共找到"+totalRecord+"条相关记录");
					sw.WriteLine("创建时间:"+System.DateTime.Now.ToString());

					string question="";
					string answer="";
					returnValue.Append("<b>共找到"+totalRecord+"条相关记录,每页显示"+pageCount+"条</b><br>");
					for(int i=curLine;i<pageCount+curLine;i++)
					{
						if(i<totalRecord)
						{
							question=dt.Rows[i]["content"].ToString();
							answer=dt.Rows[i]["answerContent"].ToString();
							
							//写入文件
							sw.WriteLine("------------------------------------------------------------------");
							sw.Write("问题"+(i-curLine+1)+":\r\n"+this.htmlDecode(question)+"\r\n");
							sw.WriteLine("------------------------------------------------------------------");
							sw.Write("问题"+(i-curLine+1)+"回答:\r\n"+this.htmlDecode(answer)+"\r\n");
							sw.WriteLine("------------------------------------------------------------------");

							question=this.removeTag(question);//删除文本中的HTML标签
							answer=this.removeTag(answer);

							if(question.Length>100)
								question=question.Substring(0,100)+"...";
							if(answer.Length>100)
								answer=answer.Substring(0,100)+"...";

							if(keyword!="")
							{
								//进行高亮显示
								question=this.lightShow(question,keyword);
								answer=this.lightShow(answer,keyword);
							}

							returnValue.Append(Convert.ToString(i+1)+"、"+viewPage+dt.Rows[i]["id"].ToString()+" target=\"_blank\"><b>"+dt.Rows[i]["subject"].ToString()+"["+dt.Rows[i]["accessCount"].ToString()+"]</b></a><br>");
							returnValue.Append(viewPage+dt.Rows[i]["id"].ToString()+" target=\"_blank\">"+question+"<br>"+answer+"</a><br><br>");
						}
						else
							break;
					}
					//显示分页导航条
					
					if(totalPage>1)
					{
						returnValue.Append("转到第");
						for(int i=1;i<=totalPage;i++)
						{
							returnValue.Append("<a href="+showResultPage+"?keyword="+keyword+"&type="+type+"&page="+i.ToString()+">"+i.ToString()+"</a>&nbsp;");
						}
						returnValue.Append("页");
					}
					
					sw.Close();

					return returnValue.ToString();
				}
				return "";
			}
			catch(Exception ee)
			{
				errorString=ee.Message;
				return "";
			}
		}

		public string lightShow(string input,string keyword)
		{
			//在input中加亮显示keyword
			try
			{
				if((bool)this.getSystemPara("wordbookAllow"))
				{
					keyword+=","+this.scanWordbook(keyword);
				}
				string[] split=keyword.Split(new char[]{',',' ','&'});
				for(int i=0;i<split.Length;i++)
				{
					if(split[i].Trim()!="")
					{
						input=input.Replace(split[i],"<font style=\"BACKGROUND-COLOR: #000080\" color=\"#ffffff\">"+split[i]+"</font>");
					}
				}
				return input;
			}
			catch(Exception ee)
			{
				errorString=ee.ToString();
				return "";
			}
		}

		public string hotSearchWord(int count)
		{
			//得到最热的搜索词
			//count:返回的个数

			StringBuilder sb=new StringBuilder();
			if(count<=0)
				count=3;
			string showSearchPage="<a href=search.aspx?type=0&keyword=";
			try
			{
				DataTable dt=this.getDataTable("select * from [searchWord] order by useCount desc");
				if(dt.Rows.Count>0)
				{
					sb.Append("<b>热门搜索:</b>");
					for(int i=0;i<count;i++)
					{
						if(i<dt.Rows.Count)
						{
							sb.Append("&nbsp;&nbsp;"+showSearchPage+HttpContext.Current.Server.UrlEncode(dt.Rows[i]["keyword"].ToString().Trim())+">"+dt.Rows[i]["keyword"].ToString().Trim()+"</a>");
						}
						else
							break;
					}
				}
				return sb.ToString();
			}
			catch(Exception ee)
			{
				errorString=ee.Message;
				return "";
			}
		}

		public string aboutSearchWord(string keyword,int count)
		{
			//得到相关搜索词
			//keyword:搜索词
			//count:返回的个数

			StringBuilder sb=new StringBuilder();
			keyword=keyword.Trim();
			//string copyKeyword=keyword;
			int c=0;//传入关键字的个数

			if(keyword!="")
			{
				try
				{
					string showSearchPage="<a href=search.aspx?type=0&keyword=";
					if(count<=0)
						count=20;
					string[] split=keyword.Trim().Split(new char[]{' ',',','&'});
					c=split.Length;

					for(int i=0;i<c;i++)
					{
						if(split[i].Trim()!="")
						{
							keyword+=","+this.scanWordbook(split[i]);
						}
					}
					split=keyword.Trim().Split(new char[]{' ',',','&'});
					
					//从搜索词表中得到相关搜索词
					string sql="";
					for(int i=0;i<split.Length;i++)
					{
						//string sql="select * from [searchWord] where keyword like '%"+copyKeyword+"%'";
						if(split[i].Trim()!="")
						{
							sql="select * from [searchWord] where keyword like '%"+split[i]+"%'";
							DataTable dt=this.getDataTable(sql);
							foreach(DataRow row in dt.Rows)
							{
								if(keyword.IndexOf(row["keyword"].ToString().Trim())<0)
								{
									keyword+=","+row["keyword"].ToString().Trim();
								}
							}
						}
					}
					split=keyword.Trim().Split(new char[]{' ',',','&'});

					if(split.Length>c)
					{
						sb.Append("<b>相关搜索:</b>");
						for(int i=c;i<split.Length;i++)
						{
							if(split[i].Trim()!="")
							{
								sb.Append("&nbsp;&nbsp;"+showSearchPage+HttpContext.Current.Server.UrlEncode(split[i].Trim())+">"+split[i].Trim()+"</a>");
							}
						}
					}
				}
				catch(Exception ee)
				{
					errorString=ee.Message;
				}
			}
			return sb.ToString();
		}

		public void addToWordbook(string keyword)
		{
			//把搜索词添加到字典
			//支持添加多个,用逗号,&或空格分开
			//keyword:传入的关键字列表
			keyword=keyword.Trim();
			string sql="";
			DataTable dt=new DataTable();
			if(keyword!="")
			{
				try
				{
					string[] split=keyword.Split(new char[]{' ',',','&'});
					for(int i=0;i<split.Length;i++)
					{
						if(split[i].Trim()!="")
						{
							sql="select * from [searchWord] where keyword='"+split[i]+"'";
							dt=this.getDataTable(sql);
							if(dt.Rows.Count>0)
							{
								//数据表已经有该记录,则增加数量,如果达到设定的数目,加入到字典
								sql="update [searchWord] set useCount=useCount+1 where keyword='"+split[i]+"'";
								this.updateDatabase(sql);//使用数加一
								if(Convert.ToInt32(dt.Rows[0]["added"].ToString().Trim())==0)
								{
									//还没有加入字典,检查是否可以加入
									if(Convert.ToInt32(dt.Rows[0]["useCount"])>=(Convert.ToInt32(this.getSystemPara("addToWordbookCount"))-1))
									{
										//加入到字典表
										if(!this.catchRecord("select * from [wordbook] where wbValue='"+split[i]+"'"))
										{
											sql="insert into [wordbook](wbValue) values('"+split[i]+"')";
											this.updateDatabase(sql);
										}
										//改变状态为已加入字典
										sql="update [searchWord] set [added]='1' where keyword='"+split[i]+"'";
										this.updateDatabase(sql);
									}
								}
							}
							else
							{
								//没有记录,加入
								sql="insert into [searchWord](keyword) values('"+split[i]+"')";
								this.updateDatabase(sql);
								if(Convert.ToInt32(this.getSystemPara("addToWordbookCount"))==1)
								{
									//加入到字典表
									if(!this.catchRecord("select * from [wordbook] where wbValue='"+split[i]+"'"))
									{
										sql="insert into [wordbook](wbValue) values('"+split[i]+"')";
										this.updateDatabase(sql);
									}
									//改变状态为已加入字典
									sql="update [searchWord] set [added]='1' where keyword='"+split[i]+"'";
									this.updateDatabase(sql);
								}
							}
						}
					}
				}
				catch(Exception ee)
				{
					errorString=ee.Message;
				}
			}
		}

		#endregion

		#region 相关文本转换操作
		public string pathToString(string pathStr,string webPath)
		{
			//把类别路径转为字符串
			//从board表中检索
			//pathStr:类ID字符串,如:1,3,5
			//webPath:显示栏目的网页地址
			string returnValue="";
			string sql;
			pathStr=pathStr.Trim();
			try
			{
				if(pathStr.StartsWith("0,"))
				{
					pathStr=pathStr.Substring(2);
				}
				else if(pathStr.Trim()=="0")
				{
					pathStr="";
				}
				if(pathStr.Length>0)
				{
					string[] split=pathStr.Split(new char[]{','});
					for(int i=0;i<split.Length;i++)
					{
						sql="select * from [board] where boardID="+split[i].ToString();
						DataTable dataTable=this.getDataTable(sql);
						if(dataTable.Rows.Count>0)
						{
							returnValue+="<a href="+webPath+"?boardID="+split[i].ToString()+">"+dataTable.Rows[0]["boardName"].ToString().Trim()+"</a>>";
						}
					}
				}
				if(returnValue.Length==0)
					returnValue="<a href='default.aspx'>网上答疑系统</a>";
				else
					returnValue="<a href='default.aspx'>网上答疑系统</a>>"+returnValue.Substring(0,returnValue.Length-1);
				return returnValue;
			}
			catch(Exception ee)
			{
				errorString=ee.ToString();
				return "ERROR";
			}
		}

		public string htmlencode(string content)
		{
			//对传入的字符串进行解析,允许进行相关的格式显示
			//content:要解析的字符串
			content=HttpContext.Current.Server.HtmlEncode(content);
			content=content.Replace("\n","<br>");
			content=content.Replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;");
			content=content.Replace(" ","&nbsp;&nbsp;");
			content=content.Replace("[b]","<b>");
			content=content.Replace("[/b]","</b>");
			content=content.Replace("[u]","<u>");
			content=content.Replace("[/u]","</u>");
			content=content.Replace("[i]","<i>");
			content=content.Replace("[/i]","</i>");
			content=content.Replace("[p]","<p>");
			content=content.Replace("[/p]","</p>");
			content=content.Replace("[P]","<P>");
			content=content.Replace("[/P]","</P>");
			content=content.Replace("[STRONG]","<STRONG>");
			content=content.Replace("[/STRONG]","</STRONG>");
			content=content.Replace("[strong]","<strong>");
			content=content.Replace("[/strong]","</strong>");
			content=content.Replace("[hr]","<hr>");
			content=content.Replace("[br]","<br>");
			content=content.Replace("[center]","<center>");
			content=content.Replace("[/center]","</center>");
			content=content.Replace("[h1]","<h1>");
			content=content.Replace("[/h1]","</h1>");
			content=content.Replace("[h2]","<h2>");
			content=content.Replace("[/h2]","</h2>");
			content=content.Replace("[h3]","<h3>");
			content=content.Replace("[/h3]","</h3>");
			content=content.Replace("[h4]","<h4>");
			content=content.Replace("[/h4]","</h4>");
			content=content.Replace("[h5]","<h5>");
			content=content.Replace("[/h5]","</h5>");
			content=content.Replace("[h6]","<h6>");
			content=content.Replace("[/h6]","</h6>");
			content=content.Replace(",",",");
			content=this.myReplace(content,"[a");
			content=this.myReplace(content,"[font");
			content=this.myReplace(content,"[table");
			content=this.myReplace(content,"[tr");
			content=this.myReplace(content,"[td");
			content=this.myReplace(content,"[A");
			content=this.myReplace(content,"[FONT");
			content=this.myReplace(content,"[TABLE");
			content=this.myReplace(content,"[TR");
			content=this.myReplace(content,"[TD");
			content=this.myReplace(content,"[img");
			content=this.myReplace(content,"[IMG");
			content=this.myReplace(content,"[div");
			content=this.myReplace(content,"[DIV");
			return content;
		}

		public string myReplace(string input,string oldValue)
		{
			//替换方法,主要实现把[a href=...][/a]替换成<a href=...></a>
			//input:被操作的字符串
			//oldValue:要被替换的字符串
			//newValue:要替换成的字符串
			//返回替换后的字符串
			//200406141541
			string tem="";
			string returnValue=input;
			string tag=oldValue.Substring(1);
			int startPos=-1;
			int endPos=-1;
			startPos=input.IndexOf(oldValue);
			if(startPos>-1)
			{
				endPos=input.IndexOf("[/"+tag+"]",startPos);

				if(endPos>-1)
				{
					endPos+=2+tag.Length;
					tem=input.Substring(startPos,endPos-startPos+1);
					tem=tem.Replace("[","<");
					tem=tem.Replace("]",">");
					tem=tem.Replace("&nbsp;"," ");
					returnValue=input.Substring(0,startPos)+tem;
					if(endPos<input.Length)
					{
						returnValue+=input.Substring(endPos+1);
					}
				}
				else if(tag=="img" || tag=="IMG")
				{
					endPos=input.IndexOf("]",startPos);
					tem=input.Substring(startPos,endPos-startPos+1);
					tem=tem.Replace("[","<");
					tem=tem.Replace("]",">");
					tem=tem.Replace("&nbsp;"," ");
					returnValue=input.Substring(0,startPos)+tem;
					if(endPos<input.Length)
					{
						returnValue+=input.Substring(endPos+1);
					}
				}
			}
			return returnValue;
		}
		public string removeTag(string html)
		{
			//去掉HTML中的标签
			//html:输入的HTML方本
			//返回去掉标签后的文本
			//200406132132
			int tagStartPos=-1;
			int tagEndPos=-1;
			while(html.IndexOf("<")>-1)
			{
				tagStartPos=html.IndexOf("<");
				tagEndPos=html.IndexOf(">",tagStartPos);
				html=html.Substring(0,tagStartPos)+html.Substring(tagEndPos+1);
			}
			return html;
		}

		public string htmlDecode(string input)
		{
			//反编码
			//200405181517
			input=input.Replace("&nbsp;"," ");
			input=input.Replace("&NBSP;"," ");
			input=input.Replace("<br>","\r\n");
			input=input.Replace("<br />","\r\n");
			input=input.Replace("<BR>","\r\n");
			input=input.Replace("<BR />","\r\n");
			input=input.Replace("<p>","\r\n");
			input=input.Replace("</p>","\r\n");
			input=input.Replace("<P>","\r\n");
			input=input.Replace("</P>","\r\n");
			input=this.removeTag(input);
			input=HttpContext.Current.Server.HtmlDecode(input);
			return input;
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -