How can I prevent undesirable conversions done by ADO.NET?

T

TomTom

Hi,

I have a simple command.ExecuteNonQuery() to insert some data into the SQL
DB. The data can be inserted, but the characters like '\n' are somehow
stripped during the process. I want to keep it.

My code looks like below.

SqlCommand insertCommand = new SqlCommand();
SqlCommand command = new SqlCommand();
command.CommandText = "up_parm_insert_data_into_sql"; // This stored
procedure includes a very simple INSERT statement.
command.CommandType = CommandType.StoredProcedure;
SqlParameter [] sqlParams = new SqlParameter[1];

sqlParams[0] =
command.Parameters.Add("@myParameterText",SqlDbType.NVarChar);
sqlParams[0].Direction = ParameterDirection.Input;

// dataRow["myDataColumn"].ToString() incluedes the following text. Please
note that the text includes '\n'.
// Merry Cristmas. \n Wish you happy programming in 2005."
sqlParams[0].Value = dataRow["myDataColumn"].ToString();
command.Connection = sqlConnection;
command.ExecuteNonQuery();


Does someone know how I can preserve \n in the SQL DB? During the insert
process it appears to be lost.

TomTom
 
S

Sylvain Lafontaine

How can you tell that the \n have been striped? For example, you print the
length of the string and it is shorter by two caracters?

S. L.
 
T

TomTom

Thanks for your reply. I see the \n is stripped when I view the row in Query
Analyzer. Instead of \n, a white space is inserted. The white space appears
to be a real white space (not tab character or \n or anything else).

Sylvain Lafontaine said:
How can you tell that the \n have been striped? For example, you print
the length of the string and it is shorter by two caracters?

S. L.

TomTom said:
Hi,

I have a simple command.ExecuteNonQuery() to insert some data into the
SQL
DB. The data can be inserted, but the characters like '\n' are somehow
stripped during the process. I want to keep it.

My code looks like below.

SqlCommand insertCommand = new SqlCommand();
SqlCommand command = new SqlCommand();
command.CommandText = "up_parm_insert_data_into_sql"; // This stored
procedure includes a very simple INSERT statement.
command.CommandType = CommandType.StoredProcedure;
SqlParameter [] sqlParams = new SqlParameter[1];

sqlParams[0] =
command.Parameters.Add("@myParameterText",SqlDbType.NVarChar);
sqlParams[0].Direction = ParameterDirection.Input;

// dataRow["myDataColumn"].ToString() incluedes the following text.
Please
note that the text includes '\n'.
// Merry Cristmas. \n Wish you happy programming in 2005."
sqlParams[0].Value = dataRow["myDataColumn"].ToString();
command.Connection = sqlConnection;
command.ExecuteNonQuery();


Does someone know how I can preserve \n in the SQL DB? During the insert
process it appears to be lost.

TomTom
 
T

TomTom

A little update.
textInserted.Replace(Environment.NewLine,"\n");

This did not insert \n in the table either. In my case, it's important to
represent the line break as \n. The data is mainly consumed by SQL Query
Analyzer. The people who query the text needs to see the line break (or \n).


TomTom said:
Thanks for your reply. I see the \n is stripped when I view the row in
Query Analyzer. Instead of \n, a white space is inserted. The white space
appears to be a real white space (not tab character or \n or anything
else).

Sylvain Lafontaine said:
How can you tell that the \n have been striped? For example, you print
the length of the string and it is shorter by two caracters?

S. L.

TomTom said:
Hi,

I have a simple command.ExecuteNonQuery() to insert some data into the
SQL
DB. The data can be inserted, but the characters like '\n' are somehow
stripped during the process. I want to keep it.

My code looks like below.

SqlCommand insertCommand = new SqlCommand();
SqlCommand command = new SqlCommand();
command.CommandText = "up_parm_insert_data_into_sql"; // This stored
procedure includes a very simple INSERT statement.
command.CommandType = CommandType.StoredProcedure;
SqlParameter [] sqlParams = new SqlParameter[1];

sqlParams[0] =
command.Parameters.Add("@myParameterText",SqlDbType.NVarChar);
sqlParams[0].Direction = ParameterDirection.Input;

// dataRow["myDataColumn"].ToString() incluedes the following text.
Please
note that the text includes '\n'.
// Merry Cristmas. \n Wish you happy programming in 2005."
sqlParams[0].Value = dataRow["myDataColumn"].ToString();
command.Connection = sqlConnection;
command.ExecuteNonQuery();


Does someone know how I can preserve \n in the SQL DB? During the insert
process it appears to be lost.

TomTom
 
J

Jon Skeet [C# MVP]

TomTom said:
A little update.
textInserted.Replace(Environment.NewLine,"\n");

This did not insert \n in the table either. In my case, it's important to
represent the line break as \n. The data is mainly consumed by SQL Query
Analyzer. The people who query the text needs to see the line break (or \n).

Did you just call it as above? If so, you need to understand that
strings are immutable - the call to Replace doesn't change the string
it's called on, it returns a new string with the replacements made.
 
T

TomTom

Yes, I understand how to use the Replacement method. I put the modified text
in the variable. The earlier posting was misleading. Sorry!
 
T

TomTom

I tried it again and now it work! I had to use the escaping character
correctly. Thank you for your help!

TomTom said:
Yes, I understand how to use the Replacement method. I put the modified
text in the variable. The earlier posting was misleading. Sorry!
 
S

Sylvain Lafontaine

Do you mean by that writing \\n instead of \n ?

S. L.

TomTom said:
I tried it again and now it work! I had to use the escaping character
correctly. Thank you for your help!
 

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

Similar Threads


Top