BCA / B.Tech 14 min read

Using Stored Procedures

Stored Procedures in .NET in Hindi | Stored Procedures in .NET in Hindi:


  • A stored procedure is a powerful tool for database management.
  • It is used not only to improve performance, but it also increases code reuse and security.
  • Using it with .NET and C# is simple and effective.
  • All aspects of Stored Procedures are included.
  • This includes their introduction, use, execution, the process of sending parameters, and an example with C# in .NET.
Introduction to Stored Procedures in Hindi | Introduction to Stored Procedures:

  • A stored procedure is a database object that is a collection of one or more SQL statements programmed in SQL Server.
  • It is stored in the database and can be executed whenever needed.
  • The main use of a stored procedure is to fetch, update, or perform other operations on data from the database.
  • These improve performance, make code reusability possible, and increase database security.
Advantages of Stored Procedures in Hindi | Advantages of Stored Procedures

  • Improved performance: SQL Server pre-compiles and optimizes the stored procedure.
  • Code reuse: There is no need to write frequently used code again and again.
  • Improved security: Data security can be increased by allowing access only to the procedure.
  • Less network traffic: Instead of sending multiple SQL statements at once, only the procedure name and parameters are sent.
Using Stored Procedures in Hindi | Using Stored Procedures:

A stored procedure is defined in SQL Server and is called using the EXEC command.

How to Create Stored Procedures? How to create a stored procedure?

A stored procedure is created using the CREATE PROCEDURE command.

Syntax:

CREATE PROCEDURE Procedure_Name
AS
BEGIN
    -- SQL Statements
END

An example:

CREATE PROCEDURE GetAllEmployees
AS
BEGIN
    SELECT * FROM Employees;
END;

Executing a Stored Procedure in Hindi | Executing a Stored Procedure:

  • A stored procedure can be executed through SQL Server Management Studio (SSMS) or through code.
  • The EXEC or EXECUTE command is used to call it.

Example:

EXEC GetAllEmployees;

Sending Parameters to Stored Procedures in Hindi | Sending Parameters to a Stored Procedure:

It is possible to pass parameters in a stored procedure. Parameters can be of two types:

  • Input parameter: To receive data from the user.
  • Output parameter: To return a result.
  • Example: Procedure with an input parameter

CREATE PROCEDURE GetEmployeeByID
    @EmployeeID INT
AS
BEGIN
    SELECT * FROM Employees WHERE ID = @EmployeeID;
END;

Executing:

  • EXEC GetEmployeeByID @EmployeeID = 101;
  • Using Stored Procedures in .NET with C#
  • To use a stored procedure in .NET, the SqlConnection, SqlCommand, and SqlDataReader classes are used.
Example:

using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "Server=YourServerName;Database=YourDatabaseName;Trusted_Connection=True;";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            try
            {
                connection.Open();

                // Name of the stored procedure
                string procedureName = "GetEmployeeByID";

                using (SqlCommand command = new SqlCommand(procedureName, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    // Sending parameters
                    command.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int)).Value = 101;

                    // Reading data
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine($"ID: {reader["ID"]}, Name: {reader["Name"]}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}
With the above example, you can easily implement stored procedures in your projects.