由多个表SQL Server / Visual Studio组成的VIEW中的UPDATE / DELETE行

时间:2013-09-28 08:47:11

标签: asp.net sql gridview view multiple-tables

问:我创建的视图由多个带主键的表组成。我在ASP.NET中使用GridView来更新和删除数据库中的一行。该行由两个不同的表组成。我如何使用ASP.NET中的GridView更新和删除它?我很难搞清楚,因为我做的每一次尝试,SQL Server都说“无法更新视图,因为它正在访问多个表”。

事实证明,当VIEW由多个表组成时,您无法在GridView的“选择数据搜索”中自动生成UPDATE和DELETE语句。

请帮帮我们:(

    CREATE VIEW [dbo].[viewProcedureWithPrice]
    AS
    SELECT        dbo.[PROCEDURE].ProcedureID, dbo.[PROCEDURE].Name, dbo.[PROCEDURE].Type, dbo.[PROCEDURE].Status, dbo.PRICE_HISTORY.Price, 
                             dbo.PRICE_HISTORY.EffectivityDate
    FROM            dbo.[PROCEDURE] INNER JOIN
                             dbo.PRICE_HISTORY ON dbo.[PROCEDURE].ProcedureID = dbo.PRICE_HISTORY.ProcedureID

    GO

CREATE TABLE [dbo].[PROCEDURE](
    [ProcedureID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](100) NULL,
    [Type] [varchar](100) NULL,
    [Status] [varchar](20) NULL,
 CONSTRAINT [PK_PROCEDURE] PRIMARY KEY CLUSTERED 
(
    [ProcedureID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[PRICE_HISTORY](
    [HistoryID] [int] IDENTITY(1,1) NOT NULL,
    [ProcedureID] [int] NULL,
    [Price] [decimal](10, 2) NULL,
    [EffectivityDate] [date] NULL,
 CONSTRAINT [PK_PRICE_HISTORY] PRIMARY KEY CLUSTERED 
(
    [HistoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[PRICE_HISTORY]  WITH CHECK ADD  CONSTRAINT [FK_PRICE_HISTORY_PROCEDURE] FOREIGN KEY([ProcedureID])
REFERENCES [dbo].[PROCEDURE] ([ProcedureID])
GO

ALTER TABLE [dbo].[PRICE_HISTORY] CHECK CONSTRAINT [FK_PRICE_HISTORY_PROCEDURE]
GO

1 个答案:

答案 0 :(得分:0)

如果我理解了这个问题,我所做的就是在行中添加我自己的删除按钮,使用不同的命令名称,例如“MyDelete”(可能添加适当的命令参数来标识行或使用DataKeys)。在GridView_RowCommand事件中捕获它并在那里手动执行删除。

也许有更好的方法,但这很有效。

我必须看到代码更详细。