【问题标题】:Saving image to database as varbinary (silverlight)将图像作为 varbinary (silverlight) 保存到数据库
【发布时间】:2011-11-25 19:53:43
【问题描述】:

今天我一直在尝试将图像保存到数据库中,但我真的无法弄清楚。

我做了下表(Afbeeldingen):

  • id:int
  • afbeelding1:varbinary(max)

用一个 Linq-to-SQL 类导入它,为它写了一个 WCF 服务:

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

然后在我的 xaml 页面中,我尝试创建一个 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)

【问题讨论】:

    标签: database silverlight image linq-to-sql varbinary


    【解决方案1】:

    你需要构造一个Binary对象。

    a.afbeelding1 = new Binary( bytes );
    

    【讨论】:

    • 谢谢,但是当我尝试这个时,我得到了错误:'OndernemersAward.EditAfbeeldingServiceReference.Binary' does not contain a constructor that takes 1 arguments
    • 应该是System.Linq.Binary——也许你已经为它创建了一个类?
    • 我刚刚搜索了 afbeelding1,但在任何地方都找不到它(除了 linq 类),这是 linq 类:pastebin.com/n91W2fme 编辑:还有一个,在 reference.cs :pastebin.com/jDA07rUs(不知道为什么),我也不知道为什么叫afbeelding1,因为在我的数据库中它只是afbeelding。
    • 我刚刚将该列重命名为“源”,再次将其添加到我的数据类中,但它仍然告诉我它不包含带 1 个参数的构造函数。这没有任何意义:S
    • 这很奇怪。我想知道为什么它为服务创建了 Binary 类。如果它坚持这样做,请使用a.afbeelding1 = new Binary { Bytes = bytes };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多