我的SQL Server数据库中有一个大表,如下所示:
CREATE TABLE [dbo].[Servers](
[ServerId] [uniqueidentifier] NOT NULL,
[ServerName] [nvarchar](50) NULL,
[HostName] [nvarchar](50) NULL,
[OperatingSystem] [nvarchar](50) NULL,
[OSVersion] [nvarchar](50) NULL,
[OSComment] [nvarchar](200) NULL,
[ServerManufacturer] [nvarchar](50) NULL,
[ServerModel] [nvarchar](50) NULL,
[IP] [binary](4) NULL,
[Mask] [binary](4) NULL,
[Gateway] [binary](4) NULL,
[PrimaryDNS] [binary](4) NULL,
[SecondaryDNS] [binary](4) NULL,
[NetworkType] [nvarchar](20) NULL,
[NetworkComment] [nvarchar](200) NULL,
[SiteId] [uniqueidentifier] NULL,
[DataCenterRow] [smallint] NULL,
[DataCenterRack] [smallint] NULL,
[AntiVirusProduct] [nvarchar](50) NULL,
[AntiVirusVersion] [nvarchar](50) NULL,
[AntiVirusComment] [nvarchar](200) NULL,
[PassedOSSecurity] [nvarchar](50) NULL,
[Approved] [bit] NULL,
[ApprovedBy] [nvarchar](50) NULL,
CONSTRAINT [PK_Servers] PRIMARY KEY CLUSTERED
(
[ServerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
在我的C#Windows窗体应用程序中,我有一个显示该表内容的表单,并执行显示/创建/删除/更新操作。
现在,当我编辑记录中的某些字段时,我需要执行一个只更新已更新字段的查询。换句话说,我需要一个动态识别已编辑字段并更新它们的查询。
逐个更新列的存储过程如下所示:
CREATE PROCEDURE dbo.UpdateServer @ServerName nvarchar(50), @HostName nvarchar(50),@OperatingSystem nvarchar(50),
@OSVersion nvarchar(50), @OSComment nvarchar(50),@ServerManufacturer nvarchar(50),
@ServerModel nvarchar(50), @IP nvarchar(15),@Mask nvarchar(15), @Gateway nvarchar(15),
@PrimaryDNS nvarchar(15), @SecondaryDNS nvarchar(15), @NetworkType nvarchar(20),@NetworkComment nvarchar(200),
@SiteId uniqueidentifier, @DataCenterRow smallint, @DataCenterRack smallint, @AntiVirusProduct nvarchar(50),
@AntiVirusVersion nvarchar(50), @AntiVirusComment nvarchar(200), @PassedOSSecurity bit, @Approved bit,
@ApprovedBy nvarchar(50)
AS
UPDATE [SystemsInfo].[dbo].[Servers]
SET ServerName=@ServerName,HostName=@HostName,OperatingSystem=@OperatingSystem,OSVersion=@OSVersion,OSComment=@OSComment,ServerManufacturer=@ServerManufacturer,ServerModel=@ServerModel,IP=(select dbo.fnBinaryIPv4(@IP)),
Mask=(select dbo.fnBinaryIPv4(@Mask)),Gateway=(select dbo.fnBinaryIPv4(@Gateway)),PrimaryDNS=(select dbo.fnBinaryIPv4(@PrimaryDNS)),SecondaryDNS=(select dbo.fnBinaryIPv4(@SecondaryDNS)),
NetworkType=@NetworkType,NetworkComment=@NetworkComment,SiteId=@SiteId,
DataCenterRow=@DataCenterRow, DataCenterRack=@DataCenterRack, AntiVirusProduct=@AntiVirusProduct, AntiVirusVersion=@AntiVirusVersion, AntiVirusComment=@AntiVirusComment, PassedOSSecurity=@PassedOSSecurity,
Approved=@Approved,ApprovedBy=@ApprovedBy;
答案 0 :(得分:0)
我认为史蒂夫提供的评论是主要方式。
但是,另一种选择是跟踪用户在用户界面中编辑的字段(例如CellEndEdit或CellValueChanged),在c#中创建动态SQL查询代码(使用StringBuilder
和SqlParameters
)并最终执行它。