Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have developed one windows form and able to save/update/delete the data with sql database.

I used all sql expressions in form code with queries.

How to handle all sql operations through store procedures.

Regards,
Raju G
Posted

GO
SQL
ALTER proc [dbo].[AddNewPatientRecord](@PatientMob varchar(15),@PatientName varchar(50),@PatientAge int,@PatientSex varchar(1),@PatientAddress varchar(60),@FosaUser varchar(1),@DoctName varchar(30))
as
insert into Patient values(@PatientMob,@PatientName,@PatientAge,@PatientSex,@PatientAddress,@FosaUser,@DoctName)


This can be used as an example when you write stored procedure
Its populating a table of add a new patient record

Rest in c# you can use a function to use this stored procedure like

C#
public void AddNewRecord(string PatientMob, string PatientName, int PatientAge, string PatientSex, string PatientAddress, string FosaUser, string DoctName)
      {
          cmd = new SqlCommand("AddNewPatientRecord", con);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@PatientMob", SqlDbType.VarChar).Value = PatientMob;
          cmd.Parameters.AddWithValue("@PatientName", SqlDbType.VarChar).Value = PatientName;
          cmd.Parameters.AddWithValue("@PatientAge", SqlDbType.Int).Value = PatientAge;
          cmd.Parameters.AddWithValue("@PatientSex", SqlDbType.VarChar).Value = PatientSex;
          cmd.Parameters.AddWithValue("@PatientAddress", SqlDbType.VarChar).Value = PatientAddress;
          cmd.Parameters.AddWithValue("@FosaUser", SqlDbType.VarChar).Value = FosaUser;
          cmd.Parameters.AddWithValue("@DoctName", SqlDbType.VarChar).Value = DoctName;

          cmd.ExecuteNonQuery();
      }
 
Share this answer
 
Refer this link here you get you solution:

how to call stored procedure in asp.net c# code[^]

Accept as answer if help.
 
Share this answer
 
Please check bellow link

http://forums.asp.net/t/1608207.aspx/1[^]

also e.g for your help


SQL
-- =============================================
-- Author: Mukesh Ghosh
-- Create date: 09 Jul, 2013
-- Description: Save Settings
/*
  EXEC stp_Save_Settings 
    	@Key = NULL,
		@Value = NULL
*/
-- =============================================
CREATE PROCEDURE stp_Save_Settings
	@Key VARCHAR(50),
	@Value NVARCHAR(500)
AS
BEGIN
	-- SET NOCOUNT ON ADDED TO PREVENT EXTRA RESULT SETS FROM
	SET NOCOUNT ON;
	
	BEGIN TRY
    
		
    
	END TRY
	BEGIN CATCH
	
		DECLARE @ErrorMessage NVARCHAR(4000),
			    @ErrorSeverity INT,
		        @ErrorState INT

		SELECT @ErrorMessage = ERROR_MESSAGE(),
		       @ErrorSeverity = ERROR_SEVERITY(),
		       @ErrorState = ERROR_STATE();

		-- USE RAISERROR INSIDE THE CATCH BLOCK TO RETURN ERROR
		RAISERROR (@ErrorMessage, -- MESSAGE TEXT.
		           @ErrorSeverity, -- SEVERITY.
			       @ErrorState -- STATE. 
		          )
	END CATCH
    
END
GO
-- =============================================
-- Author: Mukesh Ghosh
-- Create date: 09 Jul, 2013
-- Description: Delete Settings
/*
  EXEC stp_Delete_Settings 
    
*/
-- =============================================
CREATE PROCEDURE stp_Delete_Settings
	
AS
BEGIN
	-- SET NOCOUNT ON ADDED TO PREVENT EXTRA RESULT SETS FROM
	SET NOCOUNT ON;
	
	BEGIN TRY
    
		DELETE FROM tbl_App_Settings
		WHERE 

		RETURN 1
    
	END TRY
	BEGIN CATCH
	
		DECLARE @ErrorMessage NVARCHAR(4000),
			    @ErrorSeverity INT,
		        @ErrorState INT

		SELECT @ErrorMessage = ERROR_MESSAGE(),
		       @ErrorSeverity = ERROR_SEVERITY(),
		       @ErrorState = ERROR_STATE();

		-- USE RAISERROR INSIDE THE CATCH BLOCK TO RETURN ERROR
		RAISERROR (@ErrorMessage, -- MESSAGE TEXT.
		           @ErrorSeverity, -- SEVERITY.
			       @ErrorState -- STATE. 
		          )
	END CATCH
    
END
GO
-- =============================================
-- Author: Mukesh Ghosh
-- Create date: 09 Jul, 2013
-- Description: Get Settings Details
/*
  EXEC stp_Get_SettingsDetails 
    
*/
-- =============================================
CREATE PROCEDURE stp_Get_SettingsDetails
	
AS
BEGIN
	-- SET NOCOUNT ON ADDED TO PREVENT EXTRA RESULT SETS FROM
	SET NOCOUNT ON;
	
	BEGIN TRY
    
		SELECT Key,Value
		FROM   tbl_App_Settings
		WHERE  
    
	END TRY
	BEGIN CATCH
	
		DECLARE @ErrorMessage NVARCHAR(4000),
			    @ErrorSeverity INT,
		        @ErrorState INT

		SELECT @ErrorMessage = ERROR_MESSAGE(),
		       @ErrorSeverity = ERROR_SEVERITY(),
		       @ErrorState = ERROR_STATE();

		-- USE RAISERROR INSIDE THE CATCH BLOCK TO RETURN ERROR
		RAISERROR (@ErrorMessage, -- MESSAGE TEXT.
		           @ErrorSeverity, -- SEVERITY.
			       @ErrorState -- STATE. 
		          )
	END CATCH
    
END
GO
 
Share this answer
 
v2
Comments
Maciej Los 9-Jul-13 2:14am    
Please, use formatting (above Editor window)

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