Click here to Skip to main content
15,886,799 members
Articles / Database Development / SQL Server / SQL Server 2008

Working with CLR Objects in SQL Server 2005 or Above Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
2 Aug 2009CPOL7 min read 278K   424   32  
Gives an introduction of how to create SQL CLR managed objects in SQL server.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.Native)]
public struct xudaf_AsciiSum
{
    private SqlInt32 asciiCount;

    /// <summary>
    /// Initialize will be called once.
    /// </summary>
    public void Init()
    {
        asciiCount = 0;
    }

    /// <summary>
    /// Write your main logic within this.
    /// </summary>
    /// <param name="Value">Should be the value coming from the Sql console</param>
    public void Accumulate(SqlString Value)
    {
        Char[] chr = Value.ToString().ToCharArray();
        for (int i = 0; i < chr.Length; i++)
        {
            asciiCount += Convert.ToInt32(chr[i]);
        }

    }

    /// <summary>
    /// Should call Accumulate. 
    /// </summary>
    /// <param name="Group"></param>
    public void Merge(xudaf_AsciiSum Group)
    {
        Accumulate(Group.Terminate());
    }
    /// <summary>
    /// Represents the Termination condition of the Aggregate function
    /// </summary>
    /// <returns>Return tyoe if the function</returns>
    public SqlString Terminate()
    {
        return asciiCount.ToString();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions