Click here to Skip to main content
15,881,089 members
Articles / Database Development / SQL Server
Article

Deploy and Debug CLR store procedure

Rate me:
Please Sign up or sign in to vote.
2.50/5 (6 votes)
15 Oct 20063 min read 46.4K   24   5
This article will drive you, how deploy and debug CLR store procedure

Introduction

Hi All,<o:p>

<o:p> 

Here is some cool stuff related to Deploy and Debugging of CLR Store Procedure, I will cover here how to Deploy and Debugging CLR store procedure using VS 2005.<o:p>

I suggest before reading this article please read my previous articles named “CLR store procedure vs. T-SQL store procedure” and “How to create CLR store procedure”. <o:p>

1.      While deploying CLR store procedure you must have “CREATE ASSEMBLY” permission. Once deployed on SQL Server you can see and execute CLR store procedure there and then only as sown below.

Sample screenshot

 

2.      You might observe “Test Script” folder while creating CLR store procedure in VS 2005. This folder contains “Test.sql” file, if you open and see that file it consist different section each for specific task like execution of store procedure, user defined function etc. This script file is purely made for debugging purpose. Please follow give steps to debug CLR store procedure.  <o:p>

Steps for debugging CLR store procedure,<o:p>

a.      Open “Test.sql” file and insert following code at the end of file. <o:p>

EXEC dbo.GetRoles <o:p>

--Created in previous article named “How to create CLR store procedure”  <o:p>

EXEC dbo.PrintDate 'This will go into txt file'<o:p>

b.      “PrintDate” store procedure access external resources like file system. This store procedure is writing to a file. While accessing external resources, need “TRUSTWORTHY” permission.<o:p>

c.      Fire following command to achieve “TRUSTWORTHY” permission.<o:p>

ALTER DATABASE Database_Name SET TRUSTWORTHY ON  <o:p>

d.      What follows is code for “PrintDate” CLR store procedure.<o:p>

 [Microsoft.SqlServer.Server.SqlProcedure]<o:p>

       public static void PrintDate(string name)<o:p>

        {<o:p>

            <o:p>

            SqlPipe p;<o:p>

            p = SqlContext.Pipe;<o:p>

FileIOPermission filePerm = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\TestFile.txt");<o:p>

            filePerm.Assert();<o:p>

            StreamWriter sw = new StreamWriter(@"C:\TestFile.txt");<o:p>

            sw.WriteLine(name);<o:p>

            sw.Flush();<o:p>

            sw.Close(); <o:p>

            p.Send(System.DateTime.Today.ToString());<o:p>

using (SqlConnection connection = new <o:p>

<o:p> 

SqlConnection("context connection=true"))<o:p>

            {<o:p>

            connection.Open();<o:p>

SqlCommand command = new SqlCommand("SELECT * FROM     Department", connection);<o:p>

            SqlDataReader reader = command.ExecuteReader();<o:p>

                  reader.Read();<o:p>

                  SqlContext.Pipe.Send(reader);<o:p>

            }<o:p>

<o:p> 

     }<o:p>

e.      Since “PrintDate” CLR store procedure access external resources as discussed above, we need to incorporate this change in VS 2005 before deploying CLR store procedure as shown below. Following screen shows setting of “Permission Level” as “External”.  <o:p>

<o:p>

<o:p>Sample screenshot 

f.       Now I assume you have successfully deployed CLR store procedure and incorporated respective execute command in “Test.sql”.<o:p>

g.      Mark break point in “PrintDate” function and Run the application in VS 2005. Application halt at break point like any .NET application. <o:p>

Sample screenshot    <o:p>

h.      After execution complete you can observe “TestFile.txt” get generated with text inside passed as input parameter to CLR store procedure, check out “Output” window which contains results for “GetRole” CLR store procedure and SQL statement of “PrintDate” function "SELECT * FROM     Department" as shown below.<o:p>

<o:p>

<o:p>Sample screenshot 

3.      To enable CLR on SQL Server please fire following query, on particular database.<o:p>

EXEC sp_configure 'show advanced options' , '1';<o:p>

go<o:p>

reconfigure;<o:p>

go<o:p>

EXEC sp_configure 'clr enabled' , '1'<o:p>

go<o:p>

reconfigure;<o:p>

-- Turn advanced options back off<o:p>

EXEC sp_configure 'show advanced options' , '1';<o:p>

go <o:p>

<o:p> 

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


Written By
Web Developer
India India
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

Comments and Discussions

 
GeneralDeploy and Debug CLR User Defined Functions Pin
franklinraj12-Nov-09 7:06
franklinraj12-Nov-09 7:06 
GeneralThanks Pin
sashidhar30-Oct-09 19:22
sashidhar30-Oct-09 19:22 
GeneralExcellent article Pin
Kaila.Kamlesh9-Sep-07 21:58
Kaila.Kamlesh9-Sep-07 21:58 
GeneralThanx for sharing Pin
salman_mubbashir8-Dec-06 21:48
salman_mubbashir8-Dec-06 21:48 
GeneralNon-VS deployment Pin
Not Active15-Oct-06 5:31
mentorNot Active15-Oct-06 5:31 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.