2013-01-17 159 views
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(); 
+1

1)啓動SQL Server設置2)運行你的應用程序,3)捕捉到準確的查詢,4)調試在SQL Management Studio中 –

+0

你要爲你的命令類型?我可以建議考慮一個存儲過程嗎? –

+0

@BrianP CommandType設置爲默認值,即= CommandType.Text –

回答

0

是否與此幫助點破清除它有點之間的任何差別你的SQL代碼中的VS代碼?另外,請在創建查詢時使用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... 
       } 
      } 
     } 
    } 
}