2011-11-26 81 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