我如何使用单个存储过程执行所有插入,更新,删除和选择
问题描述:
我想在单个存储过程中执行所有操作,包括插入,更新,删除和选择.我如何使用c#代码在前端调用它.我使用的是3层架构层.请帮帮我.
Hi, I want to perform all the operations insert, update, delete, and select in single stored procedure. How do I call it in front end using c# code. That to I''m using 3 tier architecture layers. please help me out.
答
Use one more parameter as a @ActionStatus INT
if(@ActionStatus=1)
//do add
else if(@ActionStatus=2)
//update
else if(@ActionStatus=3)
//do select
else if(@ActionStatus=4)
//do delete
CREATE PROCEDURE [dbo].[prc_UserAddUpdate]
(
@MCUSXintIde0 as int=0,
@MCMPXintIde0 as int=0,
@MCUSXvarFnm0 as varchar(100)='',
@MCUSXvarLnm0 as varchar(100)='',
@Action as varchar(100)='',
@strId varchar(500)='',
@ReturnVal int out
)
As
---------------------- ADD SECTION START ------------------------------------
If @Action='Add'
Begin
If exists(select 1 from tbl_MCUS where MCUSXintIde0=@MCUSXintIde0)
Begin
set @ReturnVal=0 -- Unsuccessful Addition
End
Else
Begin
Insert Into tbl_MCUS
(
MCMPXintIde0,MCUSXvarFnm0,MCUSXvarLnm0
)
values
(
@MCMPXintIde0,@MCUSXvarFnm0,@MCUSXvarLnm0
)
set @MCUSXintIde0 = @@IDENTITY
set @ReturnVal=@MCUSXintIde0 -- Successful Addition
End
End
---------------------- ADD SECTION END ---------------------------------------
---------------------- EDIT SECTION START ------------------------------------
Else If @Action='Edit'
Begin
update tbl_MCUS set
MCMPXintIde0 =@MCMPXintIde0 ,
MCUSXvarFnm0 =@MCUSXvarFnm0 ,
MCUSXvarLnm0 =@MCUSXvarLnm0
where MCUSXintIde0=@MCUSXintIde0
set @ReturnVal=@MCUSXintIde0 -- Successful Edition
End
---------------------- EDIT SECTION END --------------------------------------
---------------------- DELETE SECTION START ----------------------------------
Else If @Action='Delete'
Begin
delete from tbl_MCUS where MCUSXintIde0 in (select Items from Fun_SplitInt(@strId,'#'))
set @ReturnVal=1 -- Successful Delete
End
---------------------- DELETE SECTION END ------------------------------------
尝试此过程..
对于插入和更新,使用单个特殊字符(即CommandValues)连接所有数据,并使用CommandName(例如,Insert,Update或Delete)将其发送到过程.
Try this procedure..
For inserting and updating concatenate all the data using a single special character ie CommandValues and send it to the procedure with CommandName ie.,Insert, Update or Delete.
ALTER PROCEDURE spBooks
@CommandName VARCHAR(10),
@CommandValues VARCHAR(MAX)
AS
BEGIN
DECLARE @Data VARCHAR(MAX), @BookID INT, @Title VARCHAR(100),
@PublisherName VARCHAR(30), @PublishedDate DATETIME,
@pos1 INT, @pos2 INT, @pos3 INT, @pos4 INT
IF(@CommandName='SELECT')
BEGIN
SELECT * FROM tblBook
END
ELSE IF(@CommandName='UPDATE')
BEGIN
--Splitting all the data and storing in the variables
SET @Data=@CommandValues
SET @pos1=charindex('±', @DATA)
SET @BookID=SUBSTRING(@data, 1, @pos1-1)
SET @pos2=CHARINDEX('±', @DATA, @pos1+1)
SET @Title=SUBSTRING(@DATA, @pos1+1, @pos2-@pos1-1)
SET @pos3=CHARINDEX('±', @DATA, @pos2+1)
SET @PublisherName=SUBSTRING(@DATA, @pos2+1, @pos3-@pos2-1)
SET @pos4=CHARINDEX('±', @DATA, @pos3+1)
SET @PublishedDate=SUBSTRING(@DATA, @pos3+1, @pos4-@pos3-1)
--Updating the records based upon the BookID
UPDATE tblBook SET Title=@Title, PublisherName=@PublisherName,
PublishedDate=@PublishedDate WHERE BookID=@BookID
END
ELSE IF(@CommandName='INSERT')
BEGIN
--Splitting all the data and storing in the variables
SET @Data=@CommandValues
SET @pos1=charindex('±', @DATA)
SET @Title=SUBSTRING(@data, 1, @pos1-1)
SET @pos2=CHARINDEX('±', @DATA, @pos1+1)
SET @PublisherName=SUBSTRING(@DATA, @pos1+1, @pos2-@pos1-1)
SET @pos3=CHARINDEX('±', @DATA, @pos2+1)
SET @PublishedDate=SUBSTRING(@DATA, @pos2+1, @pos3-@pos2-1)
--Updating the records based upon the BookID
INSERT INTO tblBook VALUES(@Title, @PublisherName, @PublishedDate)
END
ELSE IF(@CommandName='DELETE')
BEGIN
DELETE FROM tblBook WHERE BookID=@CommandValues
END
END
在删除行的情况下,仅在CommandValues中发送该行的ID..
一切顺利.
In the case of deleting the row send only ID of the row in CommandValues..
All the best..