0

I have many fields which has comma separated values in it. I have to insert them in my MySql table

homeToSchool[0] = "Name";
homeToSchool[1] = "Mumbai, 400080, India";
homeToSchool[2] = "Malad, MG";

var cmdText ="INSERT INTO schoolschedule(Name, From, To) VALUES (@Name, @From, @To)";
cmd.Parameters.AddWithValue("@Name", homeToSchool[0]);
cmd.Parameters.AddWithValue("@From", homeToSchool[1]);
cmd.Parameters.AddWithValue("@To", homeToSchool[2]);
connection.Open();
cmd.ExecuteNonQuery();

I am getting error due to comma separated values.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
vaibhav shah
  • 4,939
  • 19
  • 58
  • 96
  • What is the actual error? Does it say "you can't insert comma-separated values", or perhaps something else, for example an error near ", "? – CodeCaster Apr 09 '13 at 06:59

1 Answers1

7

I bet the error message is this:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From, To) VALUES.."

Actually the problem is not with the value. It's on the column name you used and you didn't escape causing syntax error.

FROM and TO are reserved keywords. They can still be used provided that you wrapp them with backticks.

INSERT INTO schoolschedule(Name, `From`, `To`) VALUES (@Name, @From, @To)

If you have chance to alter the table, change the column name which is not a reserved keyword. This will avoid you from getting problems in the future.

John Woo
  • 258,903
  • 69
  • 498
  • 492