我试图将一个数组传递给一个方法,然后返回一个可用于 SQL WHERE 子句的字符串。我有以下内容,它工作得很好。但是有更好的方法吗?我正在寻找两个结果之一;
WHERE (ColumnName IN (12, 34, 56, 78, 90))
WHERE (ColumnName IN ('12', '34', '56', '78', '90'))
public static string setInSearchFilter(string psSearchFilter, string psColumnName,
string[] paObjectValues, bool pbIsString)
{
string lsDelimiter = "'", lsRetVal = string.Empty, lsObjectValues = string.Empty;
if (!pbIsString)
{
lsDelimiter = string.Empty;
}
if (!string.IsNullOrEmpty(psSearchFilter))
{
lsRetVal = psSearchFilter + " AND ";
}
for (int i = 0; i <= paObjectValues.GetUpperBound(0); i++)
{
lsObjectValues += lsDelimiter + paObjectValues[i] + lsDelimiter;
if (i < paObjectValues.GetUpperBound(0))
{
lsObjectValues += ", ";
}
}
return lsRetVal += "(" + psColumnName + " IN (" + lsObjectValues + "))";
}