i有一个字符串,我在其中使用string.format,如果字符串迭代为="或null,我想删除第二个参数,也想删除<<<<br/>。将形成的字符串中的AND [Source].[System.IterationPath] IN ('{1}')
。
public void GetProjectInfo(string projectname,string Iteration)
{
string querystring = string.Format("select [System.Id], [System.Title],[Story.Author],[Story.Owner],[System.AssignedTo]," +
" [System.WorkItemType],[Microsoft.VSTS.Scheduling.StoryPoints],[Microsoft.VSTS.Common.Priority]," +
"[Microsoft.VSTS.Scheduling.Effort], [Actual.Effort.Completed]" +
",[System.State]," +
"[System.IterationPath]" +
" FROM WorkItemLinks" +
" WHERE" +
" ([Source].[System.TeamProject]='{0}'" +
" and [Source].[System.WorkitemType] IN ('Feature', 'Bug', 'Product Backlog Item', 'Task')" +
" AND [Source].[System.IterationPath] IN ('{1}'))"//this line of code will be removed along with iteration parameter
+
" and ([System.Links.LinkType]='System.LinkTypes.Hierarchy-Forward')" +
" ORDER BY [System.Id] " +
" mode (Recursive)", projectname, Iteration);
}
尝试
public void GetProjectInfo(string projectname, string Iteration)
{
var query =
$@"SELECT
[System.Id], [System.Title],[Story.Author],[Story.Owner],[System.AssignedTo],
[System.WorkItemType],[Microsoft.VSTS.Scheduling.StoryPoints],[Microsoft.VSTS.Common.Priority],
[Microsoft.VSTS.Scheduling.Effort], [Actual.Effort.Completed], [System.State], [System.IterationPath]
FROM WorkItemLinks
WHERE ([Source].[System.TeamProject]='{projectname}'
AND [Source].[System.WorkitemType] IN ('Feature', 'Bug', 'Product Backlog Item', 'Task')
${(string.IsNullOrEmpty(Iteration) ? "" : $"AND [Source].[System.IterationPath] IN ('{Iteration}'))") }
AND ([System.Links.LinkType]='System.LinkTypes.Hierarchy-Forward')
ORDER BY [System.Id] mode (Recursive)";
}
注意
字符串串联是不良练习用于构建SQL查询,有SQL注入的线