Click here to Skip to main content
15,885,546 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 277.9K   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;
using System.Xml;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void xsp_ExportData()
    {
        SqlPipe pipe = SqlContext.Pipe;
        using (SqlConnection con = new SqlConnection("context connection=true"))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM USERS", con))
            {
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                XmlDocument document = new XmlDocument();
                XmlDeclaration xmlDeclaration = document.CreateXmlDeclaration("1.0", "utf-8", null);
                XmlElement rootNode = document.CreateElement("USERS");
                document.AppendChild(rootNode);
                while (reader.Read())
                {
                    XmlElement parentNode = document.CreateElement("USER");
                    parentNode.SetAttribute("ID", reader["userid"].ToString());
                    parentNode.InnerText = reader["FNAME"].ToString();
                    rootNode.AppendChild(parentNode);

                }
                pipe.Send(document.InnerXml); // this will print the XML output
                reader.Close();
                pipe.ExecuteAndSend(cmd); // This will show the result
                SqlDataRecord rec = new SqlDataRecord();
                pipe.SendResultsStart(rec);
                pipe.SendResultsRow(rec);
                pipe.SendResultsStart(rec);
                con.Close();
            }

        }
    }
       
};

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