RobertC
RobertC

Reputation: 123

C# and Oracle carriage return

I am trying to get a carriage return to happen with C# when inserting into an oracle database but I can't seem to get it correct.

I know its CHR(13) but when I run the replace on the string it replaces a specific character that i am using to mark a carriage return but when the insert happens it comes in with "CHR(13)" instead of a carriage return.

This is what I am using:

txt = txt.Replace("|", "' ||CHAR(13)|| '");

I have also tried setting a variable and giving it the calue of char(13) but same result.

INSERT INTO people (id, first_name, last_name, txt) VALUES ('" + id + "', '" + record.first_name + "', '" + record.last_name + "', '" + txt + "')";

Output:

INSERT INTO people (cust_sid, first_name, last_name, notes) VALUES ('10', 'steve', 'man','thisistext ||CHR(13)|| 07, more:more,  ||CHR(13)')

Can anyone let me know what I am doing wrong.

Thanks,

Upvotes: 0

Views: 1128

Answers (1)

Hambone
Hambone

Reputation: 16417

Your issue is the last '|' at the end of txt. It's definitely possible to triage this and do what you're trying to do, but as others have said in the comments, please don't.

Consider, what if your variable txt contains the following:

He'll do just fine

How are you going to handle the ' character, and more importantly do you really want to?

Bind variables are like bacon -- they make everything better:

// OracleConnection conn;

int id = 1;
string txt = "this is text|more:more, |";

OracleCommand cmd = new OracleCommand(
    "insert into people (id, first_name, last_name, txt) values " +
    "(:ID, :FIRST, :LAST, :TXT)", conn);

cmd.Parameters.Add("ID", id);
cmd.Parameters.Add("FIRST", "steve");
cmd.Parameters.Add("LAST", "man");
cmd.Parameters.Add("TXT", txt.Replace("|", Environment.NewLine));

cmd.ExecuteNonQuery();

If you were inserting more than a single record, you'd want to do this a little differently (declare parameters once, execute multiple times with different values), but the same basic concepts would apply.

Upvotes: 1

Related Questions