Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a string with ( ' ) in to the sql database?

Tags:

c#

sql-server

I have the strings which consists of ( ' ) quote mark like "mother's love" ...

While inserting the data by sql query from c#. It shows error. How can i rectify the problem and insert this kind of data successfully?

string str2 = "Insert into tblDesEmpOthDetails (EmpID, Interviewnotes) values ('" + EmpId + "','" + Interviewnotes + "')";

Interview notes consists the value like "Mother's love" (with single quote). While executing this query it shows error as "Unclosed quotation mark after the character string ')" how can i insert this type of strings?

like image 504
Arun Kumar Avatar asked Jun 20 '12 13:06

Arun Kumar


1 Answers

I'm pretty sure you don't use SQL parameters:

using (SqlCommand myCommand = new SqlCommand(
    "INSERT INTO table (text1, text2) VALUES (@text1, @text2)")) {

    myCommand.Parameters.AddWithValue("@text1", "mother's love");
    myCommand.Parameters.AddWithValue("@text2", "father's love");
    //...

    myConnection.Open();
    myCommand.ExecuteNonQuery();
    //...
}
like image 78
Otiel Avatar answered Oct 21 '22 20:10

Otiel