Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.
Posted
Updated 11-Mar-12 21:10pm
v2
Comments
Shahin Khorshidnia 12-Mar-12 3:29am    
1. Please explain your question. Do you have one insert, one select, one update and one delete, or many of any one?
2. What do you mean 3 tier architecture layer? Do you have 3 separate physical computers too(For example 2 servers and one client) If yes, does it have any connection with the subject on hand?! I think when you talking about tiers, then you need to think about communication and handling events, amongst the segments that can be located on a physically separate computers.
3. What have you done and what was holding it up?!

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
 
Share this answer
 
Comments
Shahin Khorshidnia 12-Mar-12 3:55am    
Hello,
I asked jjjsssaaa, some questions because the question needs more detail.
I think it's not as simple as all that. if I guess right, he/she wants:
1. How to call the stored procedure that located on SQL Server.
2. How to call business method that calls the store procedure from client side? You know that business layer is probably located on a separate computer and presentation is on another computer.
SumitChandra 30-Dec-13 6:10am    
solution 1 is the best approach... add another parameter and get/set the value in it for insert/delete or whatever you want to
jjjjjjjjjjjssssssssssssaaaaaaaaaa 12-Mar-12 9:08am    
hii ya i m talking abt 3tier architecture..there is no other seperate computer on same system....ya i got insert ,update and delete by using this solution but i m not getting select since it requires data adapter n dataset which is to be used in c# code so plss help me by posting c# code for select operation ..
SQL
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 ------------------------------------
 
Share this answer
 
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.
SQL
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


In the case of deleting the row send only ID of the row in CommandValues..

All the best..
 
Share this answer
 
USE [RupaDB]
GO
/****** Object:  StoredProcedure [dbo].[P_SP_Action]    Script Date: 01/01/2014 02:10:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[P_SP_Action]( 
	@Action varchar(20),
	@EmployeeNo varchar(20),
	@EmployeeName varchar(20),
	@DateofBirth varchar(20),
	@Gender varchar(2),
	@Age varchar(20),
	@Designation varchar(20),
	@MobileNo varchar(20),
	@State varchar(20))
as
begin
	declare @EmpId int;
	begin transaction
		set @EmpId= @@IDENTITY;
		if(@Action=1)
		begin
		--For Insert
			insert into mst_Employee(EmployeeNo,EmployeeName,DateofBirth,Gender,Age) values(@EmployeeNo,@EmployeeName,@DateofBirth,@Gender,@Age)
			insert into mst_Employee_Official(EmpId,Designation,MobileNo,State) values(@EmpId,@Designation,@MobileNo,@State)
		end
  
		--For Update
		if(@Action=2)
		begin
			update mst_Employee set EmployeeNo=@EmployeeNo,EmployeeName=@EmployeeName,@DateofBirth=@DateofBirth,Gender=@Gender,Age=@Age
			update mst_Employee_Official set EmpId=@EmpId,Designation=@Designation,MobileNo=@MobileNo,State=@State
		end
 
		--For Delete
		if(@Action=3)
		begin
			delete mst_Employee 
			delete mst_Employee_Official
		end
 
		--for Select
		if(@Action=4)
		begin
			select * from mst_Employee
			select * from mst_Employee_Official
		end
 
		if(@@ERROR=0)
			commit
		else
			rollback
		end
 
Share this answer
 
v2
Comments
CHill60 31-Dec-13 7:58am    
Some reasons for my downvote ... in the Update you do not have a WHERE clause which means you are going to update all the records in the table(s). Ditto for delete and the FROM is missing too. Also you have no parameters that are output so the select is pointless. See Solutions 1 and 2 for the correct approach
Maciej Los 31-Dec-13 8:32am    
Another reason: date of question: 12 mar 2012!
CHill60 31-Dec-13 8:39am    
Yep! I don't mind if it adds something but this ...
Maciej Los 31-Dec-13 8:43am    
It happens...
;)
Happy New Year!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900