C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Creating User Defined Functions Using Managed Code For SQL Server 2005


Posted Date: 22 Jul 2008    Resource Type: Articles    Category: .NET Framework
Author: Brainstorming GuyMember Level: Diamond    
Rating: 1 out of 5Points: 15



Most of them might be aware that we can create a Managed Code and we can deploy it in SQL Server 2005 because SQL Server 2005 has CLR Enabled with it.

In this tutorial we are going to have a look on how to create a SQL Server User Defined Function in C# and deploy that in SQL Server 2005. I have attached a file which contains the screen shots of the steps.

We have to go through few steps in achieving this.

  1. Create Project of Type DataBase and select SQL Server in the Project Template.
  2. Add an item of type User-Defined Functions.
  3. Write your code.
  4. Select Build ? Deploy <your projectName>
    1. If you have not enabled the CLR in SQL Server 2005. Before you have to enable the CLR in the SQL Server 2005.
      1. This can be done by executing the following statements in Query Window of SQL Server 2005.

      EXEC sp_configure 'clr enabled' , 1 Go
      reconfigure;
      Then select Deploy Option from the menu.


Creating Database Project


  1. Go to File --> Project
  2. Select the project type as Database and Template as SQL Server Project

  3. In the next step you will be prompted to create a connection. You will get a popup window to select one connection or create a new connection for this project. I made use of my Local SQL Server Express database and selected NORTHWIND as the database.
  4. Then right click the project and select Add ? User-Defined Functions.
    1. Select User Defined Function under Visual Studio Installed Templates.

  5. I just named the CS file as Add3Numbers.


  6. [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlInt32 Add3Numbers(int firstNumber, int secondNumber, int thirdNumber)
    {
    return firstNumber + secondNumber + thirdNumber;
    }


  7. Build the application to make sure that it’s compiling successfully.
  8. Then, select Build ? Deploy <yourprojectName>
  9. It will deploy the function successfully. You can try running the following query in Management Studio.
    1. Login to the SQL Server and select the database which you have used to create a connection and run the following statement.
    2. select dbo.Add3Numbers(20,30,40)
    3. You will get a following error if CLR is not enabled

    4. Msg 6263, Level 16, State 1, Line 1
      Execution of user code in the .NET Framework is disabled. Enable "clr enabled" configuration option.
    5. You have to run the following statement to execute the function. This has to be executed in Query Window of SQL Server 2005.
      1. EXEC sp_configure 'clr enabled' , 1
    6. This will enable the CLR in SQL Server 2005.
    7. Then execute [B]select dbo.Add3Numbers(20,30,40)[/B]

Even though it doesn’t cover an in-depth of deploying Managed Code in SQL Server, it covers a bit to get started with.

I will post some more articles on deploying managed code in SQL Server 2005 in coming days. If you have any comments/queries/suggestions, please leave here.


Regards,
Brainstorming Guy aka Venkatarajan A



Attachments

  • Images (19679-2253-Images.doc)










  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    User Defined Functions for SQL Server  .  User Defined Functions  .  User Defined Function in C#  .  Managed Code in SQL Server  .  Creating User Defined Functions for SQL Server  .  Creating User Defined Functions  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Printing DataGridView
    Previous Resource: How to Create and Use a simple Custom Control
    Return to Discussion Resource Index
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use