Solved

Escape character automatically added to strings retrieved from database

Posted on 2010-11-17
7
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 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