Reputation: 363
I am having a SQL query of the form:
Update TableName Set TaskStatus=xxx where TaskID=xx;
I have this SQL query in my C# code.
In an excel file, under B cell, I have a set of statements like Rejected, Completed, Closed and so on. My C# code reads those cells and takes appropriate actions. For example, if it reads Completed, then the below query gets executed:
Update TableName Set TaskStatus=Completed where TaskID=xx;
My problem is, in a cell, I am having the value Can't Complete. When my code reads this cell and merges this into the query,
Update TableName Set TaskStatus=Can't Complete where TaskID=xx;
it throws an error stating that "quotes should be closed" It is assuming the quote in Can't as a SQL keyword or SQL quotation. How do I get over this?
Upvotes: 1
Views: 591
Reputation: 3513
You can replace '
with ''
. The quotes define a text, so your code opens, but doesnt close the text 'area' by inserting Can't
.
Upvotes: 1
Reputation: 1161
AFAIK using parameterized SQL command solve this problem, sincs SqlParameter objects do the excaping for you in these situations. Something like this in your code should solve your problem:
string sql = "Update TableName Set TaskStatus=@status where TaskID=@id;";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("status", status_excel_call_value);
cmd.Parameters.AddWithValue("id", your_task_id);
cmd.ExecuteNonQuery();
Upvotes: 3