yash fale yash fale - 1 month ago 4x
SQL Question

how to insert newline after word in sql table in c#

I am trying to insert New Line after word car but it is not working with folowing solution

  1. Char(13)
    - not working

  2. Environment.NewLine
    - when i use this it works but appends '(' this char in sql rows like 'Car ( Rate:2CR'

  3. \n\r
    - not working


cmd.Parameters.AddWithValue("@ColumnCar", Car + "char(13)" + "Rate:2CR";
//cmd.Parameters.AddWithValue("@ColumnCar", Car + "\n\r" + "Rate:2CR";
//cmd.Parameters.AddWithValue("@ColumnCar", Car + Environment.NewLine + "Rate:2CR";


Need output in sql table
row value as follows:


Note : here after Car there will be a newline and then Rate:2Cr will be added


With the LoC Car + "char(13)" + "Rate:2CR"; you will get a literal string "char(13)" between your 2 values, not a new line. If you want only a new line you can append "\n" or you can append the character equivalent (char)10 of new line.

Now what character or string actually represents a new line might depend on your environment including the collation you are using. In simple ascii/ansi this will work. It might not be the same for another collation. As @mhasan pointed out it could also be different depending on the O/S.

Using characters

const char carriageReturn = (char) 13; // see https://en.wikipedia.org/wiki/Carriage_return
const char newLine = (char) 10;

var car = "some car";
var toInsert = car + newLine + "Rate:2CR";
cmd.Parameters.AddWithValue("@ColumnCar", toInsert);

This would also work and produce the same result:

var toInsert = car + "\n" + "Rate:2CR";