0

我正在尝试将图像添加到数据库中的表中,但在使用 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;
    }

截屏:

在此处输入图像描述

但是如果我把它改成这个,它就可以保存得很好

        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" />

4

1 回答 1

1

编辑了整个答案...

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

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

检查连接字符串“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 替换数据库连接字符串,这是不可能的。

于 2012-09-07T08:46:37.037 回答