全文查询不返回任何结果

时间:2013-01-17 03:43:40

标签: c# .net sql-server full-text-search

大家好! 如果有人能帮助我,我会很高兴。

我开发了一个全文搜索引擎,其中C#和Sql Server 2008作为数据库。 当我在SSMS中运行它时,下面的查询返回结果,但是当我从C#代码以其参数化形式运行它时,它不返回任何内容:

SqlCommand cmd = new SqlCommand("SELECT distinct occurrence "+
                                "FROM sys.dm_fts_parser('FORMSOF(INFLECTIONAL, @doc)', 1033, 0, 0) "+
                                "where display_term in "+
                                "( "+
                                  "SELECT display_term "+
                                  "FROM sys.dm_fts_parser('FORMSOF(INFLECTIONAL, @searchterm)', 1033, 0, 0) "+
                                 ")",conn);

            cmd.Parameters.Add("@searchterm",SqlDbType.VarChar);
            cmd.Parameters["@searchterm"].Value = "distributed property";
            cmd.Parameters.Add("@doc", SqlDbType.VarChar);
            cmd.Parameters["@doc"].Value = "in the search of distributed ways that are provided by properties";
            SqlDataReader dr = cmd.ExecuteReader();

1 个答案:

答案 0 :(得分:0)

使用此帮助清理一下是否可以指出您的VS代码与SQL代码之间的差异?另外,请在创建查询时使用USING和ADDWITHVALUE。我不知道它是否仅仅是我,但我认为这是必须的。

using (SqlConnection con = new SqlConnection(conString))
{
    string command = string.Format({0}{1}{2}{3}{4}{5},  
    "SELECT distinct occurrence ",
    "FROM sys.dm_fts_parser('FORMSOF(INFLECTIONAL, @doc)', 1033, 0, 0) ",
    "where display_term in "
    "( ",
    "SELECT display_term ",
    "FROM sys.dm_fts_parser('FORMSOF(INFLECTIONAL, @searchterm)', 1033, 0, 0) ",
    ")")
    using (SqlCommand cmd = new SqlCommand(command,conn))
    {
        cmd.Parameters.AddWithValue("@searchterm", "distributed property");
        cmd.Parameters.AddWithValue("@doc", "in the search of distributed ways that are provided by properties");

        if (con.State != ConnectionState.Open)
        {
            con.Open();
        }
        using (SqlDataReader dr = new SqlDataReader(cmd))
        {
            if(dr.HasRows)
            {
                While(dr.Read())
                {
                    // Do stuff here...
                }
            }
        }
    }
}