2015-09-23 82 views
-1

我正在嘗試爲我正在創建的小型數據庫編寫自定義查詢生成器,但應該出現在字符串的所有條目之間的逗號不會僅出現在結束了。從字符串中缺少逗號

private void BTN_advancedSearch_Click(object sender, EventArgs e) 
    { 
     // Creates the variable part of the custom query 
     string customwhereclause = ""; 

     if (CHK_enableGameName.Checked == true) 
     { 
      Connectqry(customwhereclause); 
      customwhereclause += "Game.GameName LIKE '%" + TXT_addGame.Text + "%'"; 
     } 

     if (CHK_enableGenreName.Checked == true) 
     { 
      Connectqry(customwhereclause); 
      customwhereclause += "Genre.GenreID =" + genreID + ""; 
     } 

     if (CHK_enableConsoleName.Checked == true) 
     { 
      Connectqry(customwhereclause); 
      customwhereclause += "Console.ConsoleID =" + consoleID + ""; 
     } 

     if (CHK_enablePlayers.Checked == true) 
     { 
      Connectqry(customwhereclause); 
      customwhereclause += "Game.Players >=" + NUD_players.Value + ""; 
     } 
     if (CHK_enableDisc.Checked == true) 
     { 
      if (CHK_discOwned.Checked == true) 
      { 
       Connectqry(customwhereclause); 
       customwhereclause += "Game.Disc ='" + "yes" + "'"; 
      } 
      else 
      { 
       Connectqry(customwhereclause); 
       customwhereclause += "Game.Disc ='" + "no" + "'"; 
      } 
     } 
     if (CHK_enableCompleted.Checked == true) 
     { 
      if (CHK_completed.Checked == true) 
      { 
       Connectqry(customwhereclause); 
       customwhereclause += "Game.Completed ='" + "yes" + "'"; 
      } 
      else 
      { 
       Connectqry(customwhereclause); 
       customwhereclause += "Game.Completed ='" + "no" + "'"; 
      } 
     } 

     //varible query code being passed back to search form. 
     frm_search.Cstmqry = customwhereclause; 

     //close the form and reopen the other one. 
     this.Close(); 
     frm_search.Show(); 
    } 

    private void Connectqry(string s) 
    { 
     if (s == "") 
     { 
      Console.WriteLine("the query is blank"); 
     } 
     else 
     { 
      s = s + " , "; 
      Console.WriteLine(s); 
     } 
    } 

輸出是目前這樣的:

the query is blank 

Game.GameName LIKE '%name%' , 

Game.GameName LIKE '%name%'Genre.GenreID =0 , 

Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0 , 

Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0Game.Players >=1 , 

Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0Game.Players >=1Game.Disc ='no' , 

我不知道爲什麼它刪除該字符串之間是逗號。在所有的條件

if (!string.IsNullOrEmpty(customwhereclause)) 
{ 
    customwhereclause += " AND "; 
} 
customwhereclause += // Your condition 

+0

您也忘記了'AND'或'OR'操作符... –

+1

您應該從Connectqry返回新的字符串並將其添加到聲明的其餘部分之前的customwhere子句中。 – Tjasun

+3

這不是刪除逗號。它們在最後,就像你寫的那樣:'s = s +「,」'如果你想在謂詞之間使用逗號,那麼你最好把它編碼,因爲'BTN_advancedSearch_Click'不會在所有。 – Rob

回答

0

您應該添加的代碼。它將在需要的地方添加一個AND運營商。


更妙的是:

private static string computeCondition(string current, string newCondition) 
{ 
    if (!string.IsNullOrEmpty(current)) 
    { 
     current += " AND "; 
    } 
    return current + newCondition; 
} 

private void BTN_advancedSearch_Click(object sender, EventArgs e) 
{ 
    // Creates the variable part of the custom query 
    string customwhereclause = ""; 

    if (CHK_enableGameName.Checked == true) 
    { 
     Connectqry(customwhereclause); 

     customwhereclause = computeCondition(customwhereclause, "Game.GameName LIKE '%" + TXT_addGame.Text + "%'"); 
    } 
    ... 

爲了避免過大碼DUP


甚至更​​好:

private void BTN_advancedSearch_Click(object sender, EventArgs e) 
{ 
    // Creates the variable part of the custom query 
    List<string> whereClausesList = new List<string>(); 

    if (CHK_enableGameName.Checked == true) 
    { 
     Connectqry(customwhereclause); 

     whereClausesList.Add("Game.GameName LIKE '%" + TXT_addGame.Text + "%'"); 
    } 
    ... 
    string.Join(" AND ", whereClausesList); 

通過Rob

的建議
+0

只需添加!在string.IsNullOrEmpty之前否定此語句。 – Tjasun

+0

不應該是'if(!string.IsNullOrEmpty(customwhereclause))''因爲如果你已經有條件在那裏你想添加它...... – colmde

+0

是的。你很棒 –

0

您的代碼無效,因爲string是不可修復的。當您執行字符串連接時,如s = s + " , ";,這不會更新s引用的對象。它將創建一個新的string並將參考分配給s。並且因爲您沒有將s作爲ref通過,所以您只更新引用的本地副本而不是原始副本。解決這個問題的正確方法是返回新的string並分配它。

private string Connectqry(string s) 
{ 
    if (s == "") 
    { 
     Console.WriteLine("the query is blank"); 
    } 
    else 
    { 
     s = s + " , "; 
     Console.WriteLine(s); 
    } 

    return s; 
} 

,並使用它像

customwhereclause = Connectqry(customwhereclause); 

至於其他的所提到的,你可能想用「AND」,而不是逗號,並使用string.JoinStringBuilder很可能會更有效,但string是不可改變的,字符串連接創建一個新的字符串是爲什麼你當前的代碼不符合你的期望。