C# 模糊查询

来源:百度知道 编辑:UC知道 时间:2024/05/30 18:29:35
做一个模糊查询的功能,现在以实现,输入s 就能把和s有关的全查出来,输入sa就能把和sa有关的全查出来,但是要是输入”s a“中间加个空格就什么也查不出来了,想得到的结果是:输入”s a“的时候能查出带s的也能带a的还有sa的全出来!!谢谢大侠指教!
附上代码:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "select * from temp_messi where name like '%" + TextBox1.Text + "%'";
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

}

public string[] getChaXunstr(string strtemp)
{
string[] stra= strtemp.Split(' ');
string strb="";
for (int i = 0;i<stra.Length;i++)
{
strb += stra[i]+"&";
}
strtemp = strb.Substring(0,strb.Length-1);
stra= strtemp.Split(' ');
strb="";
for (int i = 0;i<stra.Length;i++)
{
strb += stra[i]+"&";
}
return strb.Substring(0,strb.Length-1).Split('&');
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
string strsql = "select * from temp_messi where 1<>1";
string strkey = TextBox1.Text;
string[] key = getChaXunstr(strkey);
for (int i=0;i<key.Length;i++)
{