Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串中缺少逗号_C#_Sql_String_Comma - Fatal编程技术网

C# 字符串中缺少逗号

C# 字符串中缺少逗号,c#,sql,string,comma,C#,Sql,String,Comma,我正在尝试为我正在创建的一个小数据库编写一个自定义查询生成器,但是应该出现在字符串的所有条目之间的逗号并不是唯一出现在末尾的那个 private void BTN_advancedSearch_Click(object sender, EventArgs e) { // Creates the variable part of the custom query string customwhereclause = ""; if (CHK_

我正在尝试为我正在创建的一个小数据库编写一个自定义查询生成器,但是应该出现在字符串的所有条目之间的逗号并不是唯一出现在末尾的那个

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
在你所有的情况下。它将在任何需要的地方添加
操作符


更好的是:

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);
避免太大的代码重复


或者更好:

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);
private void BTN\u advancedSearch\u单击(对象发送方,事件参数e)
{
//创建自定义查询的变量部分
列表,其中clauseslist=新列表();
if(CHK_enableGameName.Checked==true)
{
Connectqry(海关条款);
whereClausesList.Add(“Game.GameName类似“%”+TXT_addGame.Text+“%”);
}
...
string.Join(“AND”,whereClausesList);

正如

所建议的那样,您的代码不起作用,因为
字符串
是不可更改的。当您执行字符串连接,如
s=s+“,”
这不是更新
s
引用的对象。它创建了一个新的
字符串
,并将引用分配给
s
。因为你没有将
s
作为
ref
传递,你只更新引用的本地副本,而不是原始副本。正确的解决方法是返回新的
字符串
并分配它

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.Join
StringBuilder
可能会更有效,但是
string
不可变和字符串串联创建新字符串是当前代码没有达到预期效果的原因。

您还忘记了
运算符…您应该重新urn Connectqry中的新字符串,并将其添加到语句其余部分之前的CustomWhere子句中。它不会删除逗号。它们位于末尾,正如您所写的:
s=s+“,”如果你想要在谓词之间逗号,那么,你最好把它编码进去,因为<代码> BTNY-AdvaseDetrySkyCurry< /Co> >不添加逗号。OK,我不打算过度查看。认为它已关闭。你可能还想考虑使用<代码> String。加入< <代码> >。或者至少考虑使用<代码> StringBuilder < /代码>。与字符串连接相比。只需在string.IsNullOrEmpty之前添加!即可否定此语句。如果(!string.IsNullOrEmpty(customwhereclause))
,则不应该是
,因为如果您已经在其中有一个条件,则要添加它…OP不需要逗号吗?更整洁的方式(而且很可能更高效)将子句存储在列表中,并在末尾返回
string.Join(“and”,子句);
-而不是检查表达式是否为空time@juharr这是真的,但我怀疑sql条件是否可以与comas连接起来