469,623 Members | 1,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Carriage Return and Line Feed


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.
Nov 15 '05 #1
3 127095
David N. <hu******@comcast.net> wrote:
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
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2

" David N." <hu******@comcast.net> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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
Nov 15 '05 #3
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.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Neil S. | last post: by
2 posts views Thread by John Dalberg | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.