2011-11-25 92 views
1

今天我一直在嘗試將圖像保存到數據庫中,而且我真的無法弄清楚它。以varbinary形式將圖像保存到數據庫(silverlight)

我已經作出如下表(Afbeeldingen):

  • id:int
  • afbeelding1:varbinary(max)

與LINQ到SQL類進口它,寫了一個WCF服務吧:

[OperationContract] 
    public void setAfbeelding(Afbeelding a) 
    { 
     dc.Afbeeldings.InsertOnSubmit(a); 
     dc.SubmitChanges(); 
    } 

然後在我的xaml pag e我嘗試創建Afbeelding,但我無法將Byte[]設置爲varbinary。我不知道如何做到這一點,我似乎無法找到任何有關它的信息。

OpenFileDialog openFileDialog = new OpenFileDialog(); 

openFileDialog.Filter = "JPEG files|*.jpg"; 

if (openFileDialog.ShowDialog() == true) 
{ 
    Stream stream = (Stream)openFileDialog.File.OpenRead(); 
    Byte[] bytes = new Byte[stream.Length]; 
    stream.Read(bytes, 0, (int)stream.Length); 
    string fileName = openFileDialog.File.Name; 

    Afbeelding a = new Afbeelding(); 
    a.id = 1; 
    a.afbeelding1 = new Byte{ Bytes = bytes}; 
} 

我希望有人能幫助我,因爲我真的不知道這一點。

謝謝你在前進, 托馬斯

編輯:已經解決了這個問題,當我按下按鈕,我得到一個錯誤,現在會發生什麼。

System.ServiceModel.FaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :a. The InnerException message was 'There was an error deserializing the object of type OndernemersAward.Web.Afbeelding. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'. Please see InnerException for more details. 
    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) 
    at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) 
    at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result) 
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.EditAfbeeldingServiceClientChannel.EndsetAfbeelding(IAsyncResult result) 
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingService.EndsetAfbeelding(IAsyncResult result) 
    at OndernemersAward.EditAfbeeldingServiceReference.EditAfbeeldingServiceClient.OnEndsetAfbeelding(IAsyncResult result) 
    at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result) 

回答

2

您需要構造一個Binary對象。

a.afbeelding1 = new Binary(bytes); 
+0

謝謝你,但是當我嘗試這個,我得到的錯誤:\t'「OndernemersAward.EditAfbeeldingServiceReference.Binary」不包含一個構造函數1 arguments' – Schoof

+0

這應該是一個'System.Linq.Binary' - 也許你已經創建了一個類? – tvanfosson

+0

我只是做了afbeelding1的搜索,我找不到它(但linq類),這是linq類:http://pastebin.com/n91W2fme 編輯:還有一個,在引用。 CS:http://pastebin.com/jDA07rUs(我不知道爲什麼),我也不知道爲什麼它被稱爲afbeelding1,因爲在我的數據庫中它只是afbeelding。 – Schoof

相關問題