如何通过WCF将图像添加到数据库 [英] How can I add image to the database via WCF

查看:77
本文介绍了如何通过WCF将图像添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过WCF在数据库中上传图片。我正在构建一个WP 8应用程序,它将在数据库中上传图像,然后在用户的个人资料页面上显示它。除了本教程之外,没有发现任何与WP相关的内容 。我试图遵循它,但得到错误。这是我的服务实现:

I want to upload an image in the database via WCF. I'm bulding a WP 8 app that will upload image in the database and then show it on the user's profile page. Didn't find anything related to WP except for this tutorial . I'm trying to follow it, but get errors. Here is my service implementation:

public bool Upload(Stuff picture)
        {
            
            FileStream fileStream = null;
            BinaryWriter writer = null;
            string filePath;

            try
            {
                filePath = HttpContext.Current.Server.MapPath(".") +
                           ConfigurationManager.AppSettings["PictureUploadDirectory"] +
                           picture.stuffName; 

                if (picture.stuffName != string.Empty)
                {
                    fileStream = File.Open(filePath, FileMode.Create);
                    writer = new BinaryWriter(fileStream); 
                    writer.Write(picture.stuffPhoto); //error 1,2
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
                if (writer != null)
                    writer.Close();
            }
        }



错误:

1)'System.IO.BinaryWriter的最佳重载方法匹配。 Write(string)'有一些无效的参数。

2)参数1:无法从'System.Data.Linq.Binary'转换为'string'。

3) DBML1005:不支持在类型'Stuff'的列'stuffPhoto'中DbType'VarBinary(MAX)'和Type'System.Data.Linq.Binary'之间的映射。



没有错误代码3.我将stuffPhoto(Stuff类的成员)定义为VarBinary(MAX)类型,我认为它不受LINQ to SQL类的支持。但我读到我应该在数据库中定义这种类型来转换字节数组中的图像。那些教程没有使用LINQ to SQL Classes,所以我不知道在我的情况下哪种类型是正确的。

推荐答案

首先要确保所有正确的dll都被引用。



例如类 Stuff 可能需要引用。

这就是为什么你得到的类型或命名空间名称'stPic'找不到(是你吗?缺少using指令或程序集引用?)错误。
First things first, ensure all the right dlls are referenced.

For e.g. the class Stuff probably requires a reference.
That is why you get the The type or namespace name 'stPic' could not be found (are you missing a using directive or an assembly reference?) error.


这篇关于如何通过WCF将图像添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆