我正在尝试为我正在制作的小型数据库编写自定义查询生成器,但应该出现在字符串的所有条目之间的逗号不会只出现在最后是。
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' ,
我不确定为什么删除字符串之间的逗号。
答案 0 :(得分:0)
您应该添加代码:
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
的建议答案 1 :(得分: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);
正如其他人所提到的,你可能想要使用&#34; AND&#34;而不是使用逗号,并且使用string.Join
或StringBuilder
可能会更有效,但string
是不可变的,并且字符串连接创建新字符串是您当前代码无法执行的操作你期待。