0

I need to insert a base64 string into my sql server database. It ends in an equals sign "=" and I can't seem to do it without getting an "incorrect syntax near '='" error. Also, if the string begins with a number (say, "1aBgrfe="), then I get an error "incorrect syntax near 'aBgrfe='". The data type for the column is NVARCHAR(MAX). The sql insert statement in my c# program is

INSERT INTO MYTABLE VALUES ('" + myBase64String + '")

I understand that I may have to write a stored procedure, which I've never done before. Any help is appreciated. Thanks!

2
  • 2
    If your code actually looked like that, then you would get a syntax error when you compiled it. You have the ' and the " in the wrong order after the string variable. Commented Jan 27, 2014 at 18:08
  • An extract of your code could be useful to understand the scenario that raise this error Commented Jan 27, 2014 at 18:10

2 Answers 2

2

Use a parameterized query, not a string concatenation

string cmdText = "INSERT INTO MYTABLE VALUES (@str)";
SqlCommand cmd = new SqlCommand(cmdText, connection);
connection.Open();
cmd.Parameters.AddWithValue("@str", myBase64String);
cmd.ExecuteNonQuery();

Of course you need to specify the column names if you have other columns in that table

string cmdText = "INSERT INTO MYTABLE (colForbase64string) VALUES (@str)";
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the Convert() method in sql. http://msdn.microsoft.com/en-us/library/ms187928.aspx

"INSERT INTO MYTABLE (YourColumnName) VALUES(NVARCHAR(MAX), '" + myBase64String + "')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.