Solved

Escape character automatically added to strings retrieved from database

Posted on 2010-11-17
7
729 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now