使用WCF服务将图像添加到数据库

时间:2012-09-07 08:24:31

标签: database wcf image service

我正在尝试将图像添加到数据库中的表中,但在使用WCF服务时似乎无法执行此操作

以下代码给出错误

    private CRMEntities _crm;
    private readonly Uri _uri = new Uri("http://localhost/CRMService.svc");

    //Adds a new image to database
    public bool AddImage(byte[] imagefile, int epid)
    {
        if (imagefile.Equals(null) || epid.Equals(null)) return false;
        _crm = new CRMEntities(_uri);
        var i = new Image { ImageFile = imagefile, EP_ID = epid };
        _crm.AddToImages(i);
        _crm.SaveChanges();
        return true;
    }

截图:

enter image description here

但如果我把它改成这样就可以保存好

        if (imagefile.Equals(null) || epid.Equals(null)) return false;
        var crm = new CRMData.CRMEntities(System.Configuration.ConfigurationManager.ConnectionStrings["CRMEntities"].ToString());
        var i = new CRMData.Image { ImageFile = imagefile, EP_ID = epid };
        crm.AddToImages(i);
        crm.SaveChanges();
        return true;

修改

但它适用于其他类。

private CRMEntities _crm;
    private readonly Uri _uri = new Uri("http://localhost:1677/CRMService.svc");

    //METHODS

    //SAVE
    public bool AddEmailProblem(string description, DateTime datecreated, int clientid, string mailId)
    {
        if (description == null || clientid == 0 || mailId == null) return false;
        _crm = new CRMEntities(_uri);
        var objep = new EmailProblem
        {
            Description = description,
            DateCreated = datecreated,
            CLIENT_ID = clientid,
            Mail_ID = mailId
        };
        _crm.AddToEmailProblems(objep);
        _crm.SaveChanges();
        return true;
    }

保存到数据库。

我正在使用的连接字符串是 -

<connectionStrings>
<add name="CRMEntities" connectionString="metadata=res://*/CRM.csdl|res://*/CRM.ssdl|res://*/CRM.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLExpress;Initial Catalog=CRM;Persist Security Info=True;User ID=sa;Password=Server01;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
<add name="CRM" connectionString="Data Source= .\SQLExpress;Initial Catalog=CRM;Persist Security Info=True;User ID=sa;Password=Server01" providerName="System.Data.SqlClient" />

1 个答案:

答案 0 :(得分:1)

编辑了整个答案......

private readonly Uri _uri = new Uri("http://localhost/CRMService.svc");

是服务Uri而不是数据库连接字符串!

检查配置文件中的connectionString“CRMEntities”

并打电话给你代码:

 private readonly string _connectionString= "<your actual connection string>";

    //Adds a new image to database
    public bool AddImage(byte[] imagefile, int epid)
    {
        if (imagefile.Equals(null) || epid.Equals(null)) return false;
        _crm = new CRMEntities(_connectionString);

您不能简单地用服务Uri替换数据库连接字符串,这是不可能的。