字符串中缺少逗号

Missing commas from a string

我正在尝试为我正在制作的小型数据库编写一个自定义查询生成器,但是应该出现在字符串的所有条目之间的逗号并没有只出现在最后一个是.

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

在你所有的条件下。它会在任何需要的地方添加一个 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 + "%'");
    }
    ...

避免重复代码过大


甚至更好:

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

的建议

您的代码无法正常工作,因为 string 是不可变的。当您像 s = s + " , "; 这样进行字符串连接时,这不会更新 s 引用的对象。它正在创建一个新的 string 并将引用分配给 s。并且因为您没有将 s 作为 ref 传递,所以您只是在更新引用的本地副本,而不是原始副本。解决这个问题的正确方法是 return 新的 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 是不可变的和字符串连接创建一个新字符串是您当前代码未按预期执行的原因。