Domanda

I'm building a dynamic SSIS package using C# EzAPI and I want to store the Meta Data Mapping in a SQL Server Table.

I am having trouble pulling in the delimiter ("\t" for example) and EOL Characters ("\r\n") from a table into C#.

SQL Table

Here is the line of code that I am trying to get to work:

flatFileColumn.ColumnDelimiter = columns.GetUpperBound(0) == 
   Array.IndexOf(columns, column) ? SqlValuesDictionary["EndOfLineChar"] : SqlValuesDictionary["Delimiter"];

These dictionary values are strings I retrieved from a SQL Server Data Reader like this: SqlValuesDictionary["Delimiter"] = DataReader["Delimiter"].ToString()where delimiter is the column containing "\t".

So the problem is when I read in this value from SQL Server, it appears to be escaping every quote and \ and causing the value to get changed when I really want a literal "\t" for tab delimiter.

So When I change the code to read:

flatFileColumn.ColumnDelimiter = columns.GetUpperBound(0) == 
       Array.IndexOf(columns, column) ? "\r\n" : "\t";

It works fine.

The debugger is not helping much because it appears to be displaying escapes that aren't even there so trying to replace the extra \s does nothing.

In the debugger the value of SqlValuesDictionary["Delimiter"] appears as "\"\\t\"" when the literal table value is "\t". When I change the table values to \t debugger shows it as "\\t" and it still fails.

Debugger

Can anyone point me a better direction for how to resolve this? Perhaps I am going about it the wrong way. For those concerned with SQL Injection, these tables will be strictly controlled by internal employees only.

To Clarify and Summarize: What value can I put in a SQL table and how do I retrieve it in C# to be the equivalent of var tab = "\t"

È stato utile?

Soluzione

I've had the same problem.

Found two reasonable ways to get around it:

A: Insert an actual <cr><lf> in the table:

UPDATE [EzAPI_Meta].[ezapi].[FlatFile]
   SET [RowDelimiter] = CHAR(13)+CHAR(10)

I think it's a problem that you can't see those characters when you look at the table.

B: So I endeed up with replacing the characters in my C# code:

RowDelimiter.Replace("\\r\\n", "\r\n").Replace("{CR}{LF}", "\r\n");

With this one you can enter "\r\n" or "{CR}{LF}" as plain text in your table 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top