Hi,

From a C# application I read the SQL text from a text file. The text file
contains line breaks that need to be conserved.

The text file is read by a streamreader and added to a StringBuilder object.
Each line in the text file is read, one at the time. After reading a line, I
add a carriage return/linefeed by System.Enviroment.NewLine. But carriage
returns / linefeeds are added to the StringBuilder as "/r/n". When I add the
content of the StringBuilder object to the SqlCommand.CommandText property
and run the query, the SQL fails.

I have done a similar thing in VB. In VB I added carriage returns/linefeed
by the "vbCrLf" constant. This worked fine. Line breaks were added and
maintained when passing the SQL to the SqlCommand.CommandText property - no
codes were "visible".

I hope someone knows how to handle this in C#.

Thanks,
Dorte

Re: Linebreaks in SQLCommand.CommandText (C#) by OJ

OJ
Thu Oct 21 07:15:38 CDT 2004

På Thu, 21 Oct 2004 04:25:01 -0700, skrev Dorte
<Dorte@discussions.microsoft.com>:

> Hi,
>
> From a C# application I read the SQL text from a text file. The text file
> contains line breaks that need to be conserved.

I guess it's this you are looking for:

Use SqlParameters:
StringBuilder msg = new StringBuilder();
msg.Append("...");

SqlConnection conn = new SqlConnection();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO table (message) VALUES (@message)";
cmd.Parameters.Add( "@message", msg.ToString() );

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();


Best Regards
Ole Jakob

--
Sendt med M2 - Operas revolusjonerende e-postprogram:
http://www.opera.com/m2/

Re: Linebreaks in SQLCommand.CommandText (C#) by Dorte

Dorte
Fri Oct 22 02:31:02 CDT 2004

Maybe I didn't explain myself clearly.

The SqlCommand.CommandText should contain linebreaks, as it contains several
SQL queries and "GO's". The "Go" command needs a linebreak, otherwise it
fails.
I need to preserve the linebreaks from the text file (that contains the SQL
queries ), when reading it with a StreamReader object. It works when adding
linebreaks in VB.NET (vbCrLf) but in C# System.Environment.NewLine only adds
/r/l to the text, and the SQL query fails. I need to add linebreaks in C#.

"OJ" wrote:

> PÃ¥ Thu, 21 Oct 2004 04:25:01 -0700, skrev Dorte
> <Dorte@discussions.microsoft.com>:
>
> > Hi,
> >
> > From a C# application I read the SQL text from a text file. The text file
> > contains line breaks that need to be conserved.
>
> I guess it's this you are looking for:
>
> Use SqlParameters:
> StringBuilder msg = new StringBuilder();
> msg.Append("...");
>
> SqlConnection conn = new SqlConnection();
> SqlCommand cmd = conn.CreateCommand();
> cmd.CommandText = "INSERT INTO table (message) VALUES (@message)";
> cmd.Parameters.Add( "@message", msg.ToString() );
>
> conn.Open();
> cmd.ExecuteNonQuery();
> conn.Close();
>
>
> Best Regards
> Ole Jakob
>
> --
> Sendt med M2 - Operas revolusjonerende e-postprogram:
> http://www.opera.com/m2/
>