I'm trying to insert strings into a table on my SQL server.
However when inserting a string such as : "I like 'bats'" OR "He is 'cool'" , I get an error that says "incorrect syntax near {bats OR cool}".
How could I insert strings with double single-quotes?
string a = "I am 'coding'";
string UpdateTable = string.Format(@"UPDATE TestTable
SET Column = '{0}'
WHERE Rows = 'Monday'", a);
SqlCommand command = new SqlCommand(UpdateTable, connection);
command.ExecuteReader();
I know I can hardcode the strings like this: "I like ''bats'' " , but I was wondering if there was a programmatic way to solve this.
Any help is appreciated.
EDIT :: I've Parameterized the query. It now looks something like this:
string a = "I am 'coding'";
string UpdateTable = @"UPDATE TestTable
SET Column = @a
WHERE Rows = 'Monday'";
SqlCommand command = new SqlCommand(UpdateTable, connection);
command.Parameters.AddWithValue("@a", a);
command.ExecuteNonQuery();
this approach works for me. Thank you for the help.
Should I be using a ExecuteNonQuery or a ExecuteReader in this situation, and why?