Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Escape character automatically added to strings retrieved from database

Posted on 2010-11-17
7
Medium Priority
?
749 Views
Last Modified: 2012-05-10
I am storing control characters in a table in a SQL Server database.  When I try to retrieve the values from the table, an escape character is automatically added to the values.

For example, the following values are stored in my table:
\t, \n, \r, \r\n

When I retrieve the values from the table using the code below they are changed to:
\\t, \\n, \\r, \\r\\n

How do I keep the framework from adding the escape character to an already escaped literal?

SqlCommand cmd = new SqlCommand("column_delimiter_list", conn);
cmd.CommandType = CommandType.StoredProcedure;

SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
   while (reader.Read())
   {
       list.Add(new Delimiter(reader["name"] + "", reader["value"] + ""));
   }
   reader.Close();
}
reader.Dispose();
...

Thanks.
0
Comment
Question by:ktmedlin
  • 5
  • 2
7 Comments
 
LVL 17

Expert Comment

by:OriNetworks
ID: 34161007
Maybe?


SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
   while (reader.Read())
   {
       list.Add(new Delimiter(reader["name"].ToString().Replace("\\\\","\\") + "", reader["value"].ToString().Replace("\\\\","\\")  + ""));
   }
   reader.Close();
}
reader.Dispose();
0
 

Author Comment

by:ktmedlin
ID: 34161041
I tried this:
   list.Add(new Delimiter(reader["name"] + "", reader["value"].ToString().Replace("\\\\", "\\")));
and this:
   list.Add(new Delimiter(reader["name"] + "", reader["value"].ToString().Replace(@"\\", @"\")));
but neither worked.
The string still had "\\" in its value.

Thanks.
0
 

Author Comment

by:ktmedlin
ID: 34161073
I have attached a screenshot showing the contents of the SqlDataReader before I even start reading the records.
Screen1.gif
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
OriNetworks earned 2000 total points
ID: 34161740
Maybe try replace with ascii \ either in the c# or sql query
0
 

Author Comment

by:ktmedlin
ID: 34177633
Maybe a better question would be how to store control characters in SQL Server other than using the escape sequence like im doing now (\t, \n, \r, etc).  Im using these values stored in SQL Server to then apply to the String.Split function.

while (reader.Peek() > -1)
{
    line = reader.ReadLine();
    list.Add(new DataRecord(line.Split(delimiter.Value).ToList()));
}

Any ideas?
0
 

Assisted Solution

by:ktmedlin
ktmedlin earned 0 total points
ID: 34177874
Found answer here:
http://stackoverflow.com/questions/4230638/c-storing-and-using-control-characters-for-string-split

Needed to change how the value was stored in SQL Server change control character '\t' the char(9).

Thanks.
0
 

Author Closing Comment

by:ktmedlin
ID: 34203796
Found the solution in another site but comments provided by OriNetworks led me to the solution.

Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question