Solved

Escape character automatically added to strings retrieved from database

Posted on 2010-11-17
7
728 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

24 Experts available now in Live!

Get 1:1 Help Now