Below is a parameterized query example that will solve your quote problem. Specify your actual database column data type and maximum length.
cmd.CommandText = "insert into table1(relation_n) values(@relation_n)";
cmd.Parameters.Add("@relation_n", SqlDbType.VarChar, 50).Value = str;
Parameterized queries:
- are more secure, preventing SQL injection
- eliminate the need to escape quotes within strings
- avoid the need to format date string literals in a particular way, which vary by culture
- do not require decimal separators
- improve performance by promoting plan cache reuse
- code that is cleaner and more maintainable
I suggest one avoid AddWithValue because that method infers the SQL data type from the .NET type. This can cause undesired results like full table scans when data types do not match. Also, since the length of string types is determined by the actual string length, you'll end up with many more cached plans than necessary in SQL Server, wasting memory and increased compilation cost.