2011-11-26 82 views
0

這是我的previous question的後續處理,解決了(謝謝你),但現在我陷入了另一個錯誤。將圖像保存爲數據庫作爲varbinary,arraylength(第2部分)

我想保存的圖像在我的數據庫(稱爲 'Afbeelding'),爲我做了一個表,其中excists:

  • ID:整數
  • 烴源:VARBINARY(最大值)

然後我創建了一個wcf服務來保存'Afbeelding'到數據庫。

private static DataClassesDataContext dc = new DataClassesDataContext(); 

    [OperationContract] 
    public void setAfbeelding(Afbeelding a) 
    { 
     //Afbeelding a = new Afbeelding(); 
     //a.id = 1; 
     //a.source = new Binary(bytes); 

     dc.Afbeeldings.InsertOnSubmit(a); 
     dc.SubmitChanges(); 
    } 

然後我在項目中提到服務的引用,當我按下按鈕時,我嘗試將它保存到數據庫。

private void btnUpload_Click(object sender, RoutedEventArgs e) 
    { 

     Afbeelding a = new Afbeelding(); 

     OpenFileDialog openFileDialog = new OpenFileDialog(); 

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

     if (openFileDialog.ShowDialog() == true) 
     { 
       //string imagePath = openFileDialog.File.Name; 
       //FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read); 
       //byte[] buffer = new byte[fileStream.Length]; 
       //fileStream.Read(buffer, 0, (int)fileStream.Length); 
       //fileStream.Close(); 

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


      a.id = 1; 
      a.source = new Binary { Bytes = bytes }; 

     } 


     EditAfbeeldingServiceClient client = new EditAfbeeldingServiceClient(); 

     client.setAfbeeldingCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_setAfbeeldingCompleted); 
     client.setAfbeeldingAsync(a); 
    } 

    void client_setAfbeeldingCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
    { 
     if (e.Error != null) 
      txtEmail.Text = e.Error.ToString(); 
     else 
      MessageBox.Show("WIN"); 
    } 

然而,當我這樣做,我得到以下錯誤:

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) 
    atOndernemersAward.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) 

我不知道是什麼導致了這一點,但我認爲這是與我寫的圖像的方式到數據庫? (數組長度太大大,我真的不知道怎麼改)

感謝你的幫助, 托馬斯

+0

可能重複http://stackoverflow.com/questions/3068076/wcf-service-the-maximum-array-length-quota-16384-has-been-exceeded,HTTP: //sackoverflow.com/questions/688988/configuring-a-web-service-endpoint-and-contract-from-c-sharp-code – BlackICE

回答

1

看起來像您需要更改默認讀者配額結合,設置長度爲你適當的值:

<readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880"/> 
+0

謝謝,當我將這添加到我的綁定它給了我fo錯誤:'綁定有無效的孩子readerQuotas'。 當我把它放在另一個問題中的中時,我無法再訪問我的數據庫。 (錯誤:http://localhostr.com/file/TLA34Jh/error.JPG) – Schoof

+0

我解決了它,我不得不把它放在二進制綁定。謝謝! – Schoof