![](/img/trans.png)
[英]how to handle the exception Implicit conversion from data type varchar to varbinary(max) is not allowed. while adding the data to database
[英]Error while storing bytes in database ,Implicit conversion from data type varchar to varbinary(max) is not allowed
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+ "')");
}
我將XML文件字節存儲到數據庫中,但是發生了錯誤。
不允許從數據類型varchar隱式轉換為varbinary(max)。 使用CONVERT函數運行此查詢。
我做錯了哪里,誰能幫助我。
在數據庫中, CCD
列的數據類型為varbinary(MAX)
T-SQL中的二進制值由前綴為0x
的十六進制編碼文字表示,如下所示:
INSERT INTO Foo ( Col ) VALUES ( 0xDEADBEEF )
但是,如果您通過代碼執行此操作,請對SqlParameter
使用參數化查詢,以避免注入攻擊:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.