如何使用SQL Server 2005 Express中所有记录的默认图像填充varbinary(MAX)列

1 sql sql-server image sql-server-2005-express sql-server-express

我想在使用默认图片创建新记录时填充SQL Server数据库表的varbinary(MAX)列.如何使用触发器或在这种情况下通过任何其他方式执行此操作?

这是我尝试过的T-SQL代码:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Trg_DoctorDemographic_DefaultImage]
ON [ECGManagementSystem].[dbo].[DoctorDemographic]
 AFTER INSERT  
NOT FOR REPLICATION
AS
BEGIN
  INSERT INTO[ECGManagementSystem].[dbo].[DoctorDemographic]
    (DPhoto)
    SELECT  * FROM OPENROWSET(Bulk N'D:\Programs\MSVC\Exercises\BEMS\BEMS\res\Doctor.bmp',SINGLE_BLOB)AS BLOB
END;
Run Code Online (Sandbox Code Playgroud)

JBr*_*oks 5

不,不要这样做.

如果您拥有此行的1,000行,您将拥有此图像的1,000份副本?数据库的大小将快速增长.如果要更改默认值,则必须更新1,000张图像.不好.

将1个副本存储在1个位置 - 也许是一个名为DefaultDPhoto的表.默认情况下,将图像列保留为DoctorDemographic表中的null,然后当您去检索图像时,如果此列为空,则请拉出单个副本.

编辑:

好的,首先我要制作一个存储过程,如:

create proc getDPhoto(@ID int)
as
begin
set nocount on

if exists (select 1 from DoctorDemographic where id = @ID and DPhoto is not null)
     select DPhoto from DoctorDemographic where id = @ID
else 
    select DPhoto from DefaultDPhoto 

end
Run Code Online (Sandbox Code Playgroud)

然后从这个例子在这里作为起点我会下"检索图像"改变步骤1到以下内容:

SqlCommand cmdSelect = new SqlCommand("getDPhoto");

cmdSelect.CommandType = CommandType.StoredProcedure;
Run Code Online (Sandbox Code Playgroud)