I am building a string with StringBuilder.
我正在使用StringBuilder构建一个字符串。
StringBuilder Q = new StringBuilder();
Q.Append("INSERT INTO ");
Q.Append(_lstview_item);
Q.Append(" VALUES");
Q.Append("(");
for (i = 0; i < col_no; i++)
{
Q.Append("'");
Q.Append(col_value[i]);
Q.Append("'");
Q.Append(",");
}
Q.Append(")");
string query = Q.ToString();
However, I am getting a ","
at the end of my string. I tried using
但是,我的字符串末尾有一个“,”。我试过用
string query = ext.Substring(0, ext.LastIndexOf(",") + 1);
to remove the surplus ","
, but this also removes the ")"
.
删除剩余的“,”,但这也删除了“)”。
How can I remove the last comma only?
如何仅删除最后一个逗号?
actual outcome : INSERT INTO .... VALUES('1','2','3',)
实际结果:INSERT INTO .... VALUES('1','2','3',)
desired outcome : INSERT INTO .... VALUES('1','2','3')
期望的结果:INSERT INTO .... VALUES('1','2','3')
6 个解决方案
#1
2
This:
Q.Append(")");
replace with
if (col_no > 0)
{
Q.Length--;
}
Q.Append(")");
The check if (col_no > 0)
is a little overboard, because if there is no column, the query will still fail for other reasons, but if we consider this a template on how to combine strings in a StringBuilder
, then the check is the right thing to do.
检查if(col_no> 0)是否有点过分,因为如果没有列,查询仍会因其他原因而失败,但如果我们认为这是一个关于如何在StringBuilder中组合字符串的模板,那么检查就是正确的事情。
Ah... Building a query in that way is the wrong thing to do.
啊......以这种方式构建查询是错误的。
#2
16
You can use the "Remove" method to remove a specific character at a position:
您可以使用“删除”方法删除某个位置的特定字符:
query = query.Remove(query.LastIndexOf(","), 1);
#3
1
I would suggest to remove the comma first before you add the last )
, so:
我建议在添加最后一个之前先删除逗号,所以:
for (i = 0; i < col_no; i++)
{
Q.Append("'");
Q.Append(col_value[i]);
Q.Append("'");
Q.Append(",");
}
if(col_no > 0) Q.Length --; // <-- this removes the last character
Q.Append(")");
string query = Q.ToString();
However, if you really want to create a sql-query i would strongly suggest to use sql-parameters to prevent sql-injection. So don't include the values in your sql-string.
但是,如果你真的想创建一个sql-query,我强烈建议使用sql-parameters来阻止sql-injection。所以不要在sql-string中包含这些值。
#4
0
- the correct way to achieve your goal is to use a parameterized query.
- there is the possibility to delete the last coma before putting the bracket
- the pure answer to your question can be this:
实现目标的正确方法是使用参数化查询。
在放置括号之前,有可能删除最后一个昏迷
对你的问题的纯粹答案可以是这样的:
.
string query = ext.Substring(0, ext.LastIndexOf(",")) + ext.Substring(ext.LastIndexOf(",") + 1);
or this:
string query = ext.Remove(ext.LastIndexOf(","), 1);
#5
0
Try this. simple logic
尝试这个。简单的逻辑
Check the below if condition in befor append comma line
检查以下条件是否为befor append逗号行
**if(i!=(col_no-1))
{
Q.Append(",");
}*
With your code .
用你的代码。
for (i = 0; i < col_no; i++)
{
Q.Append("'");
Q.Append(col_value[i]);
Q.Append("'");
**if(i!=(col_no-1))
{
Q.Append(",");
}**
}
#6
0
Replace
Q.Append(",");
inside the for-loop with
在for循环中用
if (i != col_no - 1)
{
Q.Append(",");
}
#1
2
This:
Q.Append(")");
replace with
if (col_no > 0)
{
Q.Length--;
}
Q.Append(")");
The check if (col_no > 0)
is a little overboard, because if there is no column, the query will still fail for other reasons, but if we consider this a template on how to combine strings in a StringBuilder
, then the check is the right thing to do.
检查if(col_no> 0)是否有点过分,因为如果没有列,查询仍会因其他原因而失败,但如果我们认为这是一个关于如何在StringBuilder中组合字符串的模板,那么检查就是正确的事情。
Ah... Building a query in that way is the wrong thing to do.
啊......以这种方式构建查询是错误的。
#2
16
You can use the "Remove" method to remove a specific character at a position:
您可以使用“删除”方法删除某个位置的特定字符:
query = query.Remove(query.LastIndexOf(","), 1);
#3
1
I would suggest to remove the comma first before you add the last )
, so:
我建议在添加最后一个之前先删除逗号,所以:
for (i = 0; i < col_no; i++)
{
Q.Append("'");
Q.Append(col_value[i]);
Q.Append("'");
Q.Append(",");
}
if(col_no > 0) Q.Length --; // <-- this removes the last character
Q.Append(")");
string query = Q.ToString();
However, if you really want to create a sql-query i would strongly suggest to use sql-parameters to prevent sql-injection. So don't include the values in your sql-string.
但是,如果你真的想创建一个sql-query,我强烈建议使用sql-parameters来阻止sql-injection。所以不要在sql-string中包含这些值。
#4
0
- the correct way to achieve your goal is to use a parameterized query.
- there is the possibility to delete the last coma before putting the bracket
- the pure answer to your question can be this:
实现目标的正确方法是使用参数化查询。
在放置括号之前,有可能删除最后一个昏迷
对你的问题的纯粹答案可以是这样的:
.
string query = ext.Substring(0, ext.LastIndexOf(",")) + ext.Substring(ext.LastIndexOf(",") + 1);
or this:
string query = ext.Remove(ext.LastIndexOf(","), 1);
#5
0
Try this. simple logic
尝试这个。简单的逻辑
Check the below if condition in befor append comma line
检查以下条件是否为befor append逗号行
**if(i!=(col_no-1))
{
Q.Append(",");
}*
With your code .
用你的代码。
for (i = 0; i < col_no; i++)
{
Q.Append("'");
Q.Append(col_value[i]);
Q.Append("'");
**if(i!=(col_no-1))
{
Q.Append(",");
}**
}
#6
0
Replace
Q.Append(",");
inside the for-loop with
在for循环中用
if (i != col_no - 1)
{
Q.Append(",");
}