SQL全文检索 打开搜索到的文件让关键字显示红色怎么做
c#和SQL2000做的全文检索,现在想让搜索打的文件打开以后,关键字显示红色怎么做,请各位大侠指较,using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
string sql = "select * from search1 where id = '" + Request.QueryString["id"] + "'";
SqlConnection connection = new SqlConnection("server=(local);database=object;uid=sa;pwd=11331144;");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Charset = "GB2312";
Response.ClearHeaders();
Response.BufferOutput = true;
Response.AddHeader("content-type", Request.QueryString["FileType"]);
Response.ContentType = "application/msword";
dr["MyImage"].ToString().Replace(TextBox1.Text, "<font color=red>" + TextBox1.Text + "</font>");
Response.BinaryWrite((byte[])dr["MyImage"]);
}
dr.Close();
connection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConn = new SqlConnection("server=(local);database=object;uid=sa;pwd=11331144;");
string seletc = "select * from search1 where contains(MyImage, '" +TextBox1.Text + "')";
SqlDataAdapter da = new SqlDataAdapter(seletc, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "search");
DataGrid1.DataSource = ds.Tables["search"].DefaultView;
DataGrid1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
string str;
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
str = e.Item.Cells[4].Text.ToString();
str.Replace("+ TextBox1.Text +", "<font color=red>+TextBox1.Text+</font>");
}
}
}
我刚才改了一下我的程序但还是有错调试的时候说这一句有错大家再帮忙看看谢谢
dr["MyImage"].ToString().Replace(TextBox1.Text, "<font color=red>" + TextBox1.Text + "</font>");
提示错误为 --------------------编程问答-------------------- TextBox1.text的长度是0?
错误提示是这样的,呵呵。
--------------------编程问答-------------------- 我想问问,你直接用html代码替换原来的字符串能行吗? --------------------编程问答-------------------- dr["MyImage"].ToString().Replace(TextBox1.Text, " <font color=red> " + TextBox1.Text + " </font> "); //这句看上去好像没问题。
str.Replace("+ TextBox1.Text +", " <font color=red> +TextBox1.Text+ </font> ");
//这句有问题
实在不行尝试使用js替换搜索的文本。 --------------------编程问答-------------------- 用正则查找替换。 --------------------编程问答-------------------- 提示你的文本框里面没有内容,你查询出来了吗?或者你在前面作一个判断! --------------------编程问答-------------------- 检查是否查找到了你的数据,你的报错好象是这个意思 --------------------编程问答-------------------- 但我是获取别人输入的内容然后全文检索最后搜索出文件,因为搜索出来的可能是多个文件所以得获取客户端打开的是哪个文件的ID然后再打开该文件, --------------------编程问答-------------------- 难道是我程序的执行顺序错了吗请各位大哥帮忙谢谢 --------------------编程问答-------------------- 刚才修改了一下前台的TextBox加了一个默认文字后台程序不出问题了
但打开文件以后没效果关键字也没有替换成红色请大家帮忙看看是怎么回事?谢谢在线等
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
string sql = "select * from search1 where id = '" + Request.QueryString["id"] + "'";
SqlConnection connection = new SqlConnection("server=(local);database=object;uid=sa;pwd=11331144;");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Charset = "GB2312";
Response.ClearHeaders();
Response.BufferOutput = true;
Response.AddHeader("content-type", Request.QueryString["FileType"]);
Response.ContentType = "application/msword";
Response.BinaryWrite((byte[])dr["MyImage"]);
dr["MyImage"].ToString().Replace(TextBox1.Text, "<font color=red>" + TextBox1.Text + "</font>");
}
dr.Close();
connection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConn = new SqlConnection("server=(local);database=object;uid=sa;pwd=11331144;");
string seletc = "select * from search1 where contains(MyImage, '" +TextBox1.Text + "')";
SqlDataAdapter da = new SqlDataAdapter(seletc, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "search");
DataGrid1.DataSource = ds.Tables["search"].DefaultView;
DataGrid1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
}
补充:.NET技术 , ASP.NET