簡體   English   中英

在數據庫中存儲字節時出錯,不允許從數據類型varchar到varbinary(max)的隱式轉換

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM