也许你可以试试这个。
桌子:
PhotoTable(
PhotoID uniqueidentifier ROWGUIDCOL NOT NULL,
PhotoImage varbinary(max) FILESTREAM NULL,
)
插入:
create procedure spPhotoInsert
@PhotoID uniqueidentifier
,@sPhotoPath nvarchar(max)
,@PhotoImage varbinary(max)
as
begin
select
cast('' as varbinary(max)) PhotoImage
into
#ret1
truncate table #ret1
declare @strSql nvarchar(max) = 'select * from OPENROWSET(BULK '''
+ @sPhotoPath + ''',SINGLE_BLOB) AS PhotoImage'
insert into #ret1 EXEC(@strSql)
insert into
PhotoTable
(
PhotoID
,PhotoImage
)
select
@PhotoID
,PhotoImage
from
#ret1
drop table #ret1
end
更新:
create procedure spPhotoUpdate
@PhotoID uniqueidentifier
,@sPhotoPath nvarchar(max)
,@PhotoImage varbinary(max)
as
begin
select
cast('' as varbinary(max)) PhotoImage
into
#ret1
truncate table #ret1
declare @strSql nvarchar(max) = 'select * from OPENROWSET(BULK '''
+ @sPhotoPath + ''',SINGLE_BLOB) AS PhotoImage'
insert into #ret1 EXEC(@strSql)
update
PhotoTable
set
PhotoImage = r.PhotoImage
from
PhotoTable, #ret1 r
where
PhotoID = @PhotoID
drop table #ret1
end
删除:
create procedure PhotoDelete
@PhotoID uniqueidentifier
as
begin
delete
PhotoTable
where
PhotoID = @PhotoID
end
和观点:
CREATE VIEW vPhotoTable
AS
select
PhotoID
,'' as sPhotoPath
,PhotoImage
from
PhotoTable
在此之后,可以使用 EF 读取/写入图像,如下所示:
//InsertPhoto(sPath)
Entities db = new Entities();
vPhoto p = db.vPhotos.CreateObject();
p.PhotoID = Guid.NewGuid();
p.sPhotoPath = sPath;
db.vPhotos.AddObject(p);
db.SaveChanges();
//UpdatePhoto(PhotoID,sPath):
Entities db = new Entities();
vPhoto p = db.vPhotos.Where(x => x.PhotoID == PhotoID).Single();
p.sPhotoPath = sPath;
db.ObjectStateManager.ChangeObjectState(p, EntityState.Modified);
db.SaveChanges();
//DeletePhoto(PhotoID):
Entities db = new Entities();
vPhoto p = db.vPhotos.Where(x => x.PhotoID == PhotoID).Single();
db.vPhotos.DeleteObject(p);
db.SaveChanges();