CLR Stored Procedure and Creating It Step-by-Step
2. You can choose a reference of an existing database connection or click on Add New Reference.
3. If you select from existing references, skip step 3 else, add a new database reference as shown in the following image and click on Test Connection to test the connection.
4. On clicking the OK button, Visual Studio will ask you to enable SQL/CLR debugging on the selected connection. You can select "Yes" to enable debugging or "No" to disable the same.
5. Once the database reference and debugging option is selected, the project will be displayed in Solution Explorer. Select the project and right-click on Solution Explorer >> Click on Add >> Stored Procedure.
6. Add a new procedure from the installed templates as shown in the following screen. Give a proper name to it.
7. Once you select the template, it will create a .cs file with the content shown in the following image:
8. Add the following code to the method already created. Pass "context connection=true" as a connection string in the constructor while creating a new SqlConnection. This CLR stored procedure is going to be part of the database, so it will be the internal part of the database, and there is no need to connect the database externally. So, no need to provide the connection string that we usually provide in applications. Then Click on Build menu >> Click on Build Solution. Also, click on Build menu >> Deploy solution. This will deploy the assembly to the database for which we have made a connection initially.
9. Now, select the database >> Programmability. Right-click on Stored Procedures >> Click on Refresh. The list of Stored Procedures should show one newly added stored procedure. Also right click on Assemblies >> click on Refresh. This should show the newly added Assembly. Also, enable CLR Stored Procedure by the following query.
sp_configure 'clr enabled', 1
Run the following query to take effect or the above query.
RECONFIGURE
Now, execute the stored procedure. It should give similar results shown in the following screen:
ransact-SQL Statements
Following are the queries to create database, create table, insert records in the table, etc. --Create a new database for demo
CREATE DATABASE DbForClrDemo --Use database USE DbFo
Following are the queries to create database, create table, insert records in the table, etc.
–Create a new database for demoCREATE
–Use database
USE DbForClrDemo
–Create table for CustomerSalesInformation
CREATE
TABLE [dbo].[CustomerSalesInformation]([Id] [int] IDENTITY(1,1)
NOT
NULL,[Name] [varchar](50)
NOT
NULL,[Sales] [decimal](18, 2)
NOT
NULL
DEFAULT
((0)),CONSTRAINT [PK_CustomerSalesInformation] PRIMARY
KEY
CLUSTERED(
[Id] ASC
)WITH
(PAD_INDEX =
OFF, IGNORE_DUP_KEY =
OFF)
ON [PRIMARY])
ON [PRIMARY]–Insert dummy data to CustomerSalesInformation table
INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Virat Kothari’, 50000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Dhruval Shah’, 5000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Urvish Sheth’, 15000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Rakesh Bajania’, 25000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Dhaval Shah’, 150000)–Enable CLR Stored Procedure in database
sp_configure ‘clr enabled’, 1
–Run following statement to take effect of above statement
RECONFIGURE
–Now execute our CLR Stored Procedure. Remember "ClrDemo"
-is name of our Stored Procedure
EXEC [dbo].ClrDemorClrDemo
Conclusion
CLR Stored procedures are very sim
ransact-SQL Statements
Following are the queries to create database, create table, insert records in the table, etc. --Create a new database for demo
CREATE DATABASE DbForClrDemo --Use database USE DbFo
Following are the queries to create database, create table, insert records in the table, etc.
–Create a new database for demoCREATE
–Use database
USE DbForClrDemo
–Create table for CustomerSalesInformation
CREATE
TABLE [dbo].[CustomerSalesInformation]([Id] [int] IDENTITY(1,1)
NOT
NULL,[Name] [varchar](50)
NOT
NULL,[Sales] [decimal](18, 2)
NOT
NULL
DEFAULT
((0)),CONSTRAINT [PK_CustomerSalesInformation] PRIMARY
KEY
CLUSTERED(
[Id] ASC
)WITH
(PAD_INDEX =
OFF, IGNORE_DUP_KEY =
OFF)
ON [PRIMARY])
ON [PRIMARY]–Insert dummy data to CustomerSalesInformation table
INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Virat Kothari’, 50000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Dhruval Shah’, 5000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Urvish Sheth’, 15000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Rakesh Bajania’, 25000)INSERT
INTO [dbo].[CustomerSalesInformation]([Name], [Sales])
VALUES
(‘Dhaval Shah’, 150000)–Enable CLR Stored Procedure in database
sp_configure ‘clr enabled’, 1
–Run following statement to take effect of above statement
RECONFIGURE
–Now execute our CLR Stored Procedure. Remember "ClrDemo"
-is name of our Stored Procedure
EXEC [dbo].ClrDemorClrDemo
Conclusion
CLR Stored procedures are very simple and can be used in most complex scenarios.
Bibliography
References
- http://blog.digi-corp.com/2009/05/seminar-on-net-framework-and-silverlight-basics/
- http://dev.digi-corp.com/2009/06/clr-stored-procedures-and-creating-it-step-by-step/
References
- http://blog.digi-corp.com/2009/05/seminar-on-net-framework-and-silverlight-basics/
- http://dev.digi-corp.com/2009/06/clr-stored-procedures-and-creating-it-step-by-step/