如何在sql数据库表中搜索字符串

时间:2016-02-25 13:46:56

标签: c# .net sql-server

这是我的程序

public partial class message : System.Web.UI.Page
{
    string constring = ConfigurationManager.ConnectionStrings["AnonymiousSocialNetworkConnectionString"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {           
        txtuseremil.Text = Session["emid"].ToString();
    }
    protected void btnsend_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection con = new SqlConnection(constring);
        string value = txtmsg.Text;
        if (value.Contains(SqlCommand cmd = new SqlCommand("select keyword from messageanalysis where keyword=@value"))// <--MY PROBLEM          
        {
            con.Open();
            lblStatus.Text = "Normal";   <-- I WANT TO DERIVE THIS VALUE FROM TABLE ACCORDING TO THE VALUE I GET FROM keyword
            Frdsclass.Text = "Just Friend";    <--- I WANT TO DERIVE THIS VALUE FROM TABLE ACCORDING TO THE VALUE I GET FROM keyword
            SqlCommand cmd = new SqlCommand("insert into messagetable(sendid,message,userid,emotional,friendsclassify) values (@snd,@msg,@usr,@emo,@frdcl)", con);
            cmd.Parameters.AddWithValue("@snd", txtsndmail.Text);
            cmd.Parameters.AddWithValue("@msg", txtmsg.Text);
            cmd.Parameters.AddWithValue("@usr", txtuseremil.Text);
            cmd.Parameters.AddWithValue("@emo", lblStatus.Text);
            cmd.Parameters.AddWithValue("@frdcl", Frdsclass.Text);
            cmd.ExecuteNonQuery();
            con.Close();
        }

如何搜索数据库表中的单词或句子?我的方法是正确的?如果你有解决方案,请帮助

4 个答案:

答案 0 :(得分:0)

你的问题不太清楚。如果要搜索varchar字段中的子字符串,可以使用类似

进行搜索

SELECT * FROM yourtable WHERE yourvarcharfield LIKE'%yoursearchstring%'

如果您想要查看不同的字段,可以链接...或者您的其他字段,例如'%yousearchstring%'

如果要搜索以searchstring开头的记录,则条件为

..喜欢'yoursearchstring%'

答案 1 :(得分:0)

public string GetValue (string searchValue)
{
  using(SqlConnection connection = new SqlConnection(connString));
  using(SqlCommand cmd = new SqlCommand(
     "select keyword from messageanalysis where value=@value")
  {
      cmd.AddParameter("@value",searchValue);
      var result = cmd.ExecuteScalar();


      return (result == null)? null : result.ToString();     

  }
}

...

var keyword = GetValue(value);
if (keyword != null && value.Contains(keyword)){          

...

你可以这样做。我已将您的select语句分成不同的函数。这将查找您传入的值并返回关键字。如果找不到,则该函数返回null。然后我将其设置为检查并查看关键字是否为null(因为未找到null)以及它是否找到执行条件代码的值。

答案 2 :(得分:0)

试试这个......

注意:ExecuteScalar()将返回结果集的第一条记录。

    public partial class message : System.Web.UI.Page
    {
        string constring = ConfigurationManager.ConnectionStrings["AnonymiousSocialNetworkConnectionString"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {           
            txtuseremil.Text = Session["emid"].ToString();
        }
        protected void btnsend_Click(object sender, ImageClickEventArgs e)
        {
            SqlConnection con = new SqlConnection(constring);
            string value = txtmsg.Text;
            SqlCommand cmd = new SqlCommand("select keyword from messageanalysis where keyword = @value")

            cmd.Parameters.AddWithValue("@value", Keywordtextbox.text);
            cmd.Connection=con;
            con.Open();
            if (Keywordtextbox.text.Contains(cmd.ExecuteScalar()))         
            {
                lblStatus.Text = "Normal";
                Frdsclass.Text = "Just Friend";
                SqlCommand cmd = new SqlCommand("insert into messagetable(sendid,message,userid,emotional,friendsclassify) values (@snd,@msg,@usr,@emo,@frdcl)", con);
                cmd.Parameters.AddWithValue("@snd", txtsndmail.Text);
                cmd.Parameters.AddWithValue("@msg", txtmsg.Text);
                cmd.Parameters.AddWithValue("@usr", txtuseremil.Text);
                cmd.Parameters.AddWithValue("@emo", lblStatus.Text);
                cmd.Parameters.AddWithValue("@frdcl", Frdsclass.Text);
                cmd.ExecuteNonQuery();
                con.Close();
            }

答案 3 :(得分:0)

如果您的文本字段可以包含多个单词,则必须将相应数量的字符串中的单词分开,然后

选择类似saerch的记录,如我之前的回答所示

SELECT * FROM yourtable WHERE yourvarcharfield LIKE'%string1%'或yourvarcharfield LIKE'%string2%'或...

因为textfield可以有不同数量的单词,你必须用stringbuilder来构建你的sqlcommand。