database - 将图像作为 varbinary (silverlight) 保存到数据库

标签 database silverlight image linq-to-sql varbinary

今天一直想弄一张图片存入数据库,实在想不通。

我制作了下表(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)

最佳答案

你需要构建一个 Binary对象。

a.afbeelding1 = new Binary( bytes );

关于database - 将图像作为 varbinary (silverlight) 保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273662/

相关文章:

javascript - 将 javascript 计算值传递到数据库是不好的做法吗?

sql - Qry 运行时间太长,索引不起作用

image - React Native Image resizeMode vs resizeMethod

Silverlight 本地化

c# - 使用 C# 去除图像中的透明度

java - Jframe 未处理

database - 如何在数据库中表示二维数据矩阵

database - Postgresql:如果 View 被修改通知

c# - 用sql计算两点之间的距离

silverlight - 如何避免 MVVM 中的代码重复?