I want to take some long, messed up string and basically dump it in a string database field. The problem is I get all kinds of problems with the text messing up the insert query. Is there a way I can just ignore the contents and write the whole text without worrying about what's in it?
string connectionstring = @"connectionstring omitted"; // Omitted
using (SqlConnection sqlConnection = new SqlConnection(connectionstring))
{
try
{
sqlConnection.Open();
string s = " This is an email
It has very bad spacing and other special characters such as '' and (), it includes contact info like:
John@john.com
Tel :Office +(27) 082345674 3435667 / Mobile +(23)83562 4326556423
Email: john@john123.com
More complex numbers (1234) (122445123) ";
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.Connection = sqlConnection;
command.CommandText = "INSERT INTO [dbo].[Emails] ([EmailText]) VALUES('" + s + "')";
command.ExecuteNonQuery();
}
catch(Exception e)
{
throw e;
}
}
How can I save this text without the query having a problem with it?
This is the error:
System.Data.SqlClient.SqlException: 'Incorrect syntax near 've'.
An expression of non-boolean type specified in a context where a condition is expected, near 'are'.
An expression of non-boolean type specified in a context where a condition is expected, near 'have'.
Incorrect syntax near 'e'.
Incorrect syntax near 'required'.
Unclosed quotation mark after the character string ')'.'