2016-04-20 32 views
0

我有以下接口:如何將字節[]值映射到SQL類型圖像?

IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator() 
{ 
    var SQLRow = new SqlDataRecord(
     new SqlMetaData("id", SqlDbType.BigInt), 
     new SqlMetaData("image", SqlDbType.Image)); 

    foreach (ImageModel item in this) 
    { 
     SQLRow.SetSqlInt64(0, item.Id); 
     // here is the problem (this is a byte[] variable) 
     SQLRow.SetSqlByte(1, item.Image); 

     yield return SQLRow; 
    } 
} 

那麼我怎樣才能映射byte[]Image?或者,也許我可以用不同的方式存儲圖像?

謝謝。

回答

1

您應該將new SqlDataRecord(..)放在foreach聲明中,否則它將始終返回相同的對象,並且會覆蓋這些值。不知道你如何使用它,但它可能是一個問題。

要設置值時使用SQLRow.SetSqlBytes(1, new SqlBytes(...));SQLRow.SetSqlBinary(...) or SQLRow.SetBytes(...)

這是更好,如果你使用varbinary(max)varbinary(YOUR MAX LENGTH)類型用於存儲你的數據作爲image類型是MS SQL過時。 OK。

0

您可以像在SQL Server中一樣保存字節數組。這種方式更容易在應用程序中恢復。

+0

OK。性能問題如何。如果我把它保存爲'NVARCHAR(MAX)'和'Image',那麼下面是什麼? –

+0

使用byte []將產生超過nvarchar(max)的性能增益。nvarchar是指支持不同編碼的字符串。對於您的情況,您可以參考http://stackoverflow.com/questions/25400555/save-和檢索圖像二進制從 - 使用SQL服務器-實體框架-6- – Naganathan

相關問題