簡體   English   中英

使用WCF服務將圖像添加到數據庫

[英]Add image to database using WCF 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;
    }

屏幕截圖:

在此處輸入圖片說明

但是如果我將其更改為此可以節省罰款

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

編輯了整個答案...

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);

您不能簡單地用Service Uri替換數據庫連接字符串,這是不可能的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM