Introduction
In this article, we will create a CLR Stored Procedure in the following few steps:
- Start Visual Studio 2010 or later.
- Add a new project from File -> New -> Project.
- Select Database Project (provide the name).
- Creating the project requires a Database Reference (I used EmployeeDB here).
- In Solution Explorer, right-click on the project name and click Add.
- Select Stored Procedure.
- Add a new file named "myTestStoredProcedure.cs".
- The added File will look like this:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void myTestStoredProcedure()
{
//Right some code here
}
};
I am writing a code for the following three Stored Procedures:
- myTestStoredProcedure- Simply prints a message.
- spGetRolesList- Displays the rows from a table.
- spGetEmployeeList- Displays the rows from the table for a specific Age group.
The following code shows how to create a CLR-stored procedure using Visual Studio:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
///
/// Prints a Message
///
[Microsoft.SqlServer.Server.SqlProcedure]
public static void myTestStoredProcedure()
{
//Simple proc
SqlPipe objSqlPipe = SqlContext.Pipe;
objSqlPipe.Send("Hi! I am simple CLR PROC");
}
///
/// Proc to Show Rows of [EmployeeDB]..[Roles] table
///
[Microsoft.SqlServer.Server.SqlProcedure]
public static void spGetRolesList()
{
//It returns rows from Roles table
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Context Connection=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = @
"Select * from [dbo].[Roles] Order By HireDate";
conn.Open();
SqlDataReader sqldr = cmd.ExecuteReader();
SqlContext.Pipe.Send(sqldr);
sqldr.Close();
conn.Close();
}
///
/// It shows rows from Employee table on basis of supplied age
///
/// a specified age
[Microsoft.SqlServer.Server.SqlProcedure]
public static void spGetEmployeeList(Int32 intAge)
{
//It returns rows from Employee table on basis of supplied age
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Context Connection=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "Select * from [dbo].[Employees] Where Age >=@intAge Order By Age";
SqlParameter paramAge = new SqlParameter();
paramAge.Value = intAge;
paramAge.Direction = ParameterDirection.Input;
paramAge.DbType = DbType.Int32;
paramAge.ParameterName = "@intAge";
cmd.Parameters.Add(paramAge);
SqlDataReader sqldr = cmd.ExecuteReader();
SqlContext.Pipe.Send(sqldr);
sqldr.Close();
conn.Close();
}
}; 
Gaurav Kumar AroraPosted Apr 6, 2015, 9:44 AM
Abhijit Patil thanks a ton
Abhijit PatilPosted Apr 6, 2015, 5:15 AM
Awesome
Shuby AroraPosted Mar 29, 2015, 3:33 PM
Wating for new one.
Shuby AroraPosted Mar 29, 2015, 3:33 PM
Really awesome article.
Gaurav Kumar AroraPosted Mar 27, 2015, 3:42 PM
Gowtham Rajamanickam - thanks glad to read that you liked it.
Gaurav Kumar AroraPosted Mar 27, 2015, 3:42 PM
KP Singh Chundawat - thanks
Gowtham RajamanickamPosted Mar 27, 2015, 2:35 PM
good.........
K P Singh ChundawatPosted Mar 27, 2015, 2:26 PM
nice..