FAQ: How do I write objects in Managed code, the beta 1 samples/docs on the web don't work with SQL Express?

The syntax for much of SQLCLR was changed between B1 and the current builds to make it more consistent. Building SQLCLR objects in Visual Studio 2005 is really easy as we have new project types, but the Express SKUs of VS do not include this project type so the code has to be written by hand. The below is a sample from Ramachandran Venkatesh one of the PMs on the SQLCLR team in SQL Server.

-Euan Garden

Product Unit Manager

SQL Server Tools

***C# Sample***

Here is a soup-to-nuts sample to build a hello world function in C#, illustrating the basic steps of creating a source file, compiling it, registering the assembly, creating a function over it, and then invoking the function.

public class c
{
    public static string HelloWorld() { return "hello world"; }
}
save to hello.cs,

csc /t:library hello.dll hello.cs

***The following code needs to be executed in a query editor***

create assembly hello from 'c:\hello.dll'
go

create function hello() returns nvarchar(129) as external name
[hello].[c].[HelloWorld]
go

select dbo.hello()
go

Ofcourse this is just the tip of the iceberg, in terms of the functionality, but you get an idea of the basic sequence.