Carriage Return and Line Feed

D

David N.

Hi All,

I spent too much time on trying to get the CrLf into a string, which
contains embedded SQL statements that can be executed by the
SQLClient.SqlCommand. Note that these SQL statements work fine in VB.Net.
Here is a sample of the code

using System;
namespace MyTest
{ internal class SqlCommandFile
{
public static CrLf = "\r\n";
public static string[] CreateCommands =
{
"IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
OBJECT_ID(N'[dbo]." + CrLf +
"[ORGANIZATION]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)" +
CrLf +
"BEGIN" + CrLf +
" DROP TABLE [ORGANIZATION]" + CrLf +
"END" + CrLf +
" GO",
...... // Next statements....
};
}

In VB.Next, the CrLf would be replaced by vbcrlf and the statements worked
fine. However, in C#, I could not get the SQL statements work because CrLf
(or "\r\n") does not work. During debugging time, I watch the variabe
CreateCommands and noticed that the SQL statements contain exactly the text
"\r\n".

Does anyone know how to solve this carrage return line feed problem so that
the SQL will happy with the embedded statements.
 
J

Jon Skeet

David N. said:
I spent too much time on trying to get the CrLf into a string, which
contains embedded SQL statements that can be executed by the
SQLClient.SqlCommand. Note that these SQL statements work fine in VB.Net.
Here is a sample of the code

In VB.Next, the CrLf would be replaced by vbcrlf and the statements worked
fine. However, in C#, I could not get the SQL statements work because CrLf
(or "\r\n") does not work.

What exactly do you mean by "does not work"? What *does* it do?
During debugging time, I watch the variabe
CreateCommands and noticed that the SQL statements contain exactly the text
"\r\n".

Does anyone know how to solve this carrage return line feed problem so that
the SQL will happy with the embedded statements.

Your SQL *does* contain carriage return and line feed - it's just that
the debugger shows them as \r\n so that they're visible. Try writing
them out to the console, or a file, and examine the file.
 
J

Jon Davis

David N. said:
During debugging time, I watch the variabe
CreateCommands and noticed that the SQL statements contain exactly the text
"\r\n".

Have you ever debugged any simple string, "blah\r\nblah"? It will appear in
the debugger in the same way. This is because the debugger is trying to show
exactly what characters are in the string. Very handy as some strings
contain only "\n" without the "\r". You'd never know it if the debugger
showed an actual line break instead of the character codes.

Therefore, if the debugger says that a variable contains "\r\n", it means a
line break (CrLf). Otherwise it would show, "\\r\\n".

Regards,
Jon
 
B

Bharat Patel [MSFT]

Hi David,

I quickly tested out the code you have provided in a console app and it seemed
to work fine. What is the problem that you are seeing? Can you test this out
and by the way, CrLf variable is not declared as string in your code but I
changed it.

namespace ConTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
public static string CrLf = "\r\n";
public static string[] CreateCommands = {
"IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo]."
+ CrLf +
"[ORGANIZATION]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)" +
CrLf +
"BEGIN" + CrLf +
" DROP TABLE [ORGANIZATION]" + CrLf +
"END" + CrLf +
" GO",
" Second arguement to array." // Next statements....
};

[STAThread]
static void Main(string[] args)
{
Console.WriteLine(CreateCommands[0].ToString());
Console.WriteLine("Hit Enter to Close");
Console.ReadLine(); // Wait for user input

}
}
}


Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top