多分あなたはこれを試すことができます。
テーブル:
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();