0

Using .Net 4.0, C#.

I'm building custom sql in my application that is delivered to a stored procedure on the db side, and further handled from there. I need to be able to put double-quotes around my aliases for the select clause (because once you use them, you always have to use them). I have this logic created and in place, except that the stringbuilder for my select clause is not properly escaping my double-quotes.

My (cleaned) code:

StringBuilder sb = new StringBuilder();
foreach (var detail in report.CustomReport.PTS_CP_CUSTOM_REPORT_DET)
{
    var columnDataType = GetColumnDataType(detail);
    sb.Append(", " + detail.PTS_CP_CUSTOM_REPORT_VIEW_DET.COLUMN_NAME + " \"" + detail.DISPLAY_NAME + "\"");
    this.ColumnData.Add(colData);
}
return string.Format(SelectTemplate, sb);

The output select statement:

SELECT PO_NUMBER \"PO_NUMBER\", .../* several columns */, CUST_NUM \"CUST_NUM\""

I need those backslashes to disappear. I've tried several ways to fix it:

  • Append a .Replace to the return statement and the string variable that it is returned to.
  • Switch to using a verbatim (@) approach
  • Create mini string variables that hold the " \"" value, and append them instead.

I have also run the return without the string.Format() call, and the problem still occurs.

Nothing seems to work. I've looked here, here, here and a few more places and none of them seem to have an answer. Not sure where to go from here.

Community
  • 1
  • 1
Travis
  • 1,044
  • 1
  • 17
  • 36
  • What are the values being passed into sb.Append() on line 5? – FumblesWithCode May 16 '14 at 23:58
  • 3
    Where are you seeing “those backslashes”? In the VS debugger? The debugger shows strings as literals in your programming language, so if you are using C# `Hello "World"` would be shown as `"Hello \"World\""`. – Dour High Arch May 17 '14 at 00:01
  • @FumblesWithCode They're strings. Column_Name uses '_'s for spaces, Display_Name uses actual spaces. – Travis May 17 '14 at 00:05
  • @DourHighArch Yeah, I just found [this answer](http://stackoverflow.com/questions/4379262/c-sharp-stringbuilder-with-quotes-forjson) that sounds like it's saying the same thing you are, so I should pretty much just not worry about them, because they're not really there. Thanks for the second opinion, or whatever. If you add that as an answer, I'll mark it correct, when it lets me. – Travis May 17 '14 at 00:07
  • @Travis, I meant examples of values being passed in :) – FumblesWithCode May 17 '14 at 00:08
  • @FumblesWithCode Sorry - CUSTOMER_NAME and CUSTOMER NAME is one example, though it sounds like this is a phantom problem anyway. – Travis May 17 '14 at 00:10
  • 1
    That code should be fine. Only, why are you concatenating strings and when you have a string builder? I would suggest `sb.Append(", ").Append(detail.PTS_CP_CUSTOM_REPORT_VIEW_DET.COLUMN_NAME).Append(" \"").Append(detail.DISPLAY_NAME).Append("\"");`. – Guffa May 17 '14 at 00:12
  • 2
    @Guffa Per MS, string concatenation is more performant than StringBuilder when you have a fixed number of defined strings. An excerpt: `Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation.` The relevant MSDN documentation can be found [here](http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.100).aspx), under the Performance Considerations heading. So, I use the StringBuilder for the loop, and concatenation for the fixed strings. – Travis May 17 '14 at 00:19
  • 1
    Regarding concatenation performance here: it's _likely_ irrelevant. If the task is to simply build an SQL query, then undoubtedly there's going to be _far more_ work done in the actual querying and database access. Since you're building a query, I would suggest concatenating/formatting the text in such a way that's the most readable/maintainable/understandable way possible. (really, I'd suggest that normally, but when it comes to building things like queries, I think it's especially important as you're essentially trying to read/write two languages simultaneously, one of which is fragmented.) – Chris Sinclair May 17 '14 at 00:30
  • @Travis: The performance assumptions include the time for creation of the `StringBuilder` object, so it's different when you already have one. If you concatenate strings and the put that in the string bilder you are creating an intermediate string that is unneccesary. The compiler can only combine string literals, when you alter string literals and variables, the compiler can't combine them. – Guffa May 17 '14 at 01:11

0 Answers0