Solved

Escape character automatically added to strings retrieved from database

Posted on 2010-11-17
7
732 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
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!

 
LVL 17

Accepted Solution

by:
OriNetworks earned 500 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

Industry Leaders: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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