使用存储过程将映像保存到SQL Server数据库

时间:2015-03-15 09:12:48

标签: c# sql-server linq-to-sql

我使用此代码在数据库中保存图像。代码运行时没有任何错误,但不保存数据库中的数据:

    var db = new AlmostafaDataLinqDataContext();
    FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
    byte[] data = new byte[fs.Length];
    fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
    fs.Close();
    db.pEditImage(RecordID, data);

这是我的存储过程:

CREATE PROCEDURE dbo.pEditImage 
    @ID int, @Image image
AS
    UPDATE Reshteh_Data 
    SET Meyvast_image = @Image
    WHERE ID = @ID

    RETURN

我的数据库表:

CREATE TABLE [dbo].[Reshteh_Data] (
    [ID]            INT            IDENTITY (1, 1) NOT NULL,
    [ReshtehName]   NVARCHAR (255) NOT NULL,
    [Maghta]        NVARCHAR (255) NOT NULL,
    [MA_Date]       NVARCHAR (255) NULL,
    [MV_Date]       NVARCHAR (50)  NULL,
    [Country]       NVARCHAR (255) NOT NULL,
    [M_Country]     NVARCHAR (255) NULL,
    [Meyvast_image] IMAGE          NOT NULL,
    PRIMARY KEY CLUSTERED ([ID] ASC)
);

1 个答案:

答案 0 :(得分:0)

您从未正确处理过IDisposable,未保存更改并且使用文件流不必要地复杂。试试这个:

using(var db = new AlmostafaDataLinqDataContext())
{
    byte[] data = System.IO.File.ReadAllBytes(FilePath);
    db.pEditImage(RecordID, data);
    db.SubmitChanges();
}