1

每个人!如果有人可以帮助我,我会非常高兴。

我开发了一个全文搜索引擎,使用 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();
4

1 回答 1

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...
                }
            }
        }
    }
}
于 2014-03-13T17:34:24.133 回答