DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

MSH Host Function In SQL Server 2005

02.25.2006
| 2754 views |
  • submit to reddit
        // part of a sql server CLR function, and example of hosting MSH in sql server 2005
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString sqlmsh(string input)
    {
        string output;
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline(input);
        MshObject myobj =   pipeline.Invoke()[0];
        if (myobj.BaseObject is XmlDocument)
         {
            output = (myobj.BaseObject as XmlDocument).InnerXml;
        } else
            output = myobj.ImmediateBaseObject.ToString();
            
        runspace.Close();
        return new SqlString(output);
    }