Reputation: 207
public void InsertMails(string From, string To, string Date, string Content, string AttachmentPath, byte[] CCD)
{
ClassLibrary.ConnectionClass.CurrentConnection.ExecuteNonReader("Insert into RecieveDirectMails([From],[To],Date,[Content],AttachmentPath,CreationDate,LastUpdatedDate,IsReviewed,LastUpdatedBy,IsDeleted,IsLocked,LockedBy,CCD) values ('" + From + "','" + To + "','" + Date + "','" + Content + "','" + AttachmentPath + "','" + DateTime.Now + "','" + DateTime.Now + "','" + "0" + "','" + "0" + "','" + "0" + "','" + "0" + "','" + "0" + "','" + CCD+ "')");
}
I am storing XML file bytes into Database but the error occurred.
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
Where I am doing it wrong can any one help me out .
In the database the CCD
column is of datatype varbinary(MAX)
Upvotes: 0
Views: 2904
Reputation: 154995
Binary values in T-SQL are represented by hexadecimal-encoded literals prefixed with 0x
, like so:
INSERT INTO Foo ( Col ) VALUES ( 0xDEADBEEF )
However if you're doing it from code please use parameterised queries with SqlParameter
to avoid injection attacks:
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO Foo ( Col ) VALUES ( @col )";
cmd.Parameters.Add("@col", SqlDbType.VarBinary).Value = new Byte[] { 0xDE, 0xAD, 0xBE, 0xEF };
cmd.ExecuteNonQuery();
Upvotes: 1