Carriage Return and String confusion...

  • Thread starter Thread starter eric_mamet_test
  • Start date Start date
E

eric_mamet_test

I am preparing the text of a stored procedure using StringBuilder but
bumps into \r\n issues...

I use some syntax like

MyStringBuilder.Append("Something very clever here");
MyStringBuilder.Append("\r\nGO");

If I then write MyStringBuilder.ToString() into a file, it's perfect as
I have in my file

Something very clever here
GO

Great!

However, if I try to send the content of MyStringBuilder.ToString()
directly to the database engine (SQL Server), it does not work because
the "\r" and "\n" are not interpreted as Carriage Return Line Feed but
merely as "\" followed by "r" or "n"...

Another symptom of the same problem seems to be that if I run this in a
VB.Net debug window

?String.Format("*{0}*", chr(13))

I get

"*
*"

but I don't manage to do it in C#

?String.Format("*{0}*", (char)13)

gives

"*\r*"

Can somebody explain this to me, please?
 
However, if I try to send the content of MyStringBuilder.ToString()
directly to the database engine (SQL Server), it does not work because
the "\r" and "\n" are not interpreted as Carriage Return Line Feed but
merely as "\" followed by "r" or "n"...

What makes you think that?

?String.Format("*{0}*", (char)13)

gives

"*\r*"

Can somebody explain this to me, please?

It's a debugger "feature". It shows strings the way you'd write them
as literals in your code, rather than how they would be printed.



Mattias
 
Mattias said:
What makes you think that?
Well, the goal is to generate some Stored Procedure and store the sql
script in a file (for source control).

My scripts always have the same pattern, which is

IF (...)
DROP PROCEDURE ...
GO
CREATE PROCEDURE ...
....
GO

I generate the SQL script using a Stringbuilder, MySB, and extract the
final sql using

String sql = MySB.ToString();

If I use the sql string to run it straight against the database, it
fails
with "incorrect syntax near 'GO'"

If I run the script through Query Analyzer, then it works without
problem.

This is the first time I try to do this in C# while I have done it many
times in VB.Net using vbCRLF without problem.
 
Back
Top