Click here to Skip to main content
Click here to Skip to main content

How to create CLR store procedure

By , 15 Oct 2006
 

Introduction

Hi All,

 

Here is some cool stuff related to CLR Store Procedure, I will cover here how to create simple CLR store procedure using VS 2005.

I suggest before reading this article please read my previous article named “CLR store procedure vs. T-SQL store procedure” Click here.

Please perform following steps sequentially, to create CLR store procedure.

1.      Open VS 2005 and click on “New Project”, then select project type of “Database” as shown below.

Sample screenshot

 

2.      Select location for your project and say “OK”. If any connection is been created previously following screen will appear to select available database connection.

Sample screenshot

3.      You can add another connection by clicking “Add New Reference…” following screen will appear on click of button. The same screen will appear if you do not have any connection created previously.

Sample screenshot

4.      Type machine name or IP address of your SQL Server. Select type of authentication you required (here I am using windows authentication), then select database name where you would like to deploy CLR store procedure.

5.      Click on “Test Connection” if you got message “Test Connection Succeeded” then click on “OK”, else make sure you have selected proper SQL Server, database and you hold account on SQL Server for defined database.

6.      On click of “OK” you will see message window shown below.

Sample screenshot

7.      Say “Yes” and proceed, I will cover how to debug and set permissions, related to CLR store procedure in my next article named “Deploy and Debug CLR store procedure”.

8.      Now in solution click on project and say “Add New Item”, following window appears where as select item of type “Store Procedure” and click on “ADD”.  (I will cover what “Test Script” folder hold in my next article named “Deploy and Debug CLR store procedure”)

Sample screenshot

9.      Now in solution click on project and say “Add New Item”, following window appears where as select item of type “Store Procedure” and click on “ADD”.  (I will cover what “Test Script” folder hold in my next article named “Deploy and Debug CLR store procedure”)

10.  Cheer up, you are about to write your first store procedure. Here I will show you simple demo of how to select records using CLR store procedure, and it goes here.

    [Microsoft.SqlServer.Server.SqlProcedure]

    public static void GetRole()

    {

        // Put your code here

SqlConnection conn = new SqlConnection("Context    Connection=true");

SqlCommand cmd = new SqlCommand(@"Select * from dbo.Roles", conn);

 

        conn.Open();

 

        SqlDataReader rdr = cmd.ExecuteReader();

        SqlContext.Pipe.Send(rdr);

 

        rdr.Close();

        conn.Close();

  }

11.  Now select project from solution explorer and say deploy. Once deployed on SQL Server, go to SQL Server and fire following query and see the output.

     USE [IRSDev]

exec GetRole

Output :

Sample screenshot

12.  You may not able to deploy CLR store procedure in single go, since lot of permission issues. If so please refer my next article named “Deploy and Debug CLR store procedure”.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Malpani Amol
Web Developer
India India
Member
I am a .NET developer cum designer. I work for Zensar Technologies Ltd. Pune (INDIA), I consult, I develop, I debug and besides that I speak at conferences and user groups and I also write. I have written some articles on WCF, WF, SSIS, Compression decompression, CLR Store Procedure, SQL Server Reporting Services 2005, Extended grid and lot more to count.
Reach to me on:-a.malpani@zensar.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to generate CRL desplyment scriptmemberJan Prochazka alias jena20 Oct '11 - 10:36 
If you can't use Deploy function of Visual Studio:

http://www.datadmin.com/articles/how-generate-crl-installation-script
QuestionStore procedure or Stored procedure?memberSam Hobbs10 Dec '10 - 8:59 
Is this article about "Store Procedure" or "Stored Procedure"? I see the former (without a "d") many places but very few palces in the MSDN. The MSDN says "Stored Procedure" (with a "d"). I assume that "Store Procedure" is incorrect.
GeneralExcellent ArticlememberWashington600074 Nov '06 - 19:53 
I'm a complete newbie with .NET (though not with software development) and found the article very useful.
 
One small thing... explain the SQLConnection a bit more... I had to go to another site to understand exactly what needed to be there for my situation.
 
Keep up the good work.

 
William
GeneralI thought it was a good articlemembercykophysh3916 Oct '06 - 1:22 
It taught me what I wanted to know without having to go to a magazine site Smile | :)
 

 


"a fool will not learn from a wise man, but a wise man will learn from a fool"
"It is hard to fail, but it is worse never to have tried to succeed." - Theodore Roosevelt
"Success is going from failure to failure without losing your enthusiasm." - Winston Churchill


My Website || My Blog

QuestionWhat hasn't been covered beforememberMark Nischalke15 Oct '06 - 5:40 
The contents here have been covered by many others on the web, in books, magazines, etc. Sorry, but it just seems to be more of an attempt to up your article count rather than providing something of value.
 

only two letters away from being an asset

AnswerRe: What hasn't been covered beforememberMalpani Amol15 Oct '06 - 18:42 
Hey Mark I appreciate your comment. I got lot of mails indirectly points towards, how to create CLR store procedure…? And hence goes here. If you think so about article count, it’s your own view.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Oct 2006
Article Copyright 2006 by Malpani Amol
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid