0

I have the following query -

string query = "Insert into table(userId,companyID) values(" + userId + "," + SplitedLine[1] + ")";
writer.WriteLine(query);

When I am printing running this code then it is not printing the entire query in one column, rather it is breaking the query wherever there is a comma.

I tried this

How to write a value which contain comma to a CSV file in c#?

string query = "Insert into table(userId" +"\",\""+"companyID) values (" + userId + "\",\"" + SplitedLine[1] + ")";
writer.WriteLine(query);

But this is printing my insert commands in wrong format. Please help.

Anonymous
  • 113
  • 1
  • 2
  • 13
  • `string.Join`?.. – Roman Ryzhiy Dec 05 '22 at 10:58
  • Simple answer, I don't think that you can. By definition a comma indicates a new column. You will have to convert the comma into another form, say as html character `,` or `\u002C` for unicode. Consider converting the text into something else first. – ChrisBD Dec 05 '22 at 11:19
  • Why don't you use *binding variables* instead of *hardcoding* in the query? – Dmitry Bychenko Dec 05 '22 at 14:02

2 Answers2

0

I think the title of your question is ambiguous. You wanted to soround the values by quotation marks ("). But you made a mistake by escaping the " in the table part, it seams escaped " and not escaped was misked up.

Try to go with

string query = $"Insert into table(\"{userId}\",\"{companyID}\") values(\"{ userId}\",\"{SplitedLine[1]}\")";
Daniel W.
  • 938
  • 8
  • 21
0

Having tested this out, your simplest approach is to ensure that your query string is double quoted.

var query = $"\"Insert into table(userId,companyID values ({userId}, {SplitedLine[1]})\""; 
ChrisBD
  • 9,104
  • 3
  • 22
  • 35