One of the best features in SQL2005 is the integrated CLR .NET languages support. What does it mean? You can use your common .NET programming language to code stored procedures in SQL 2005. In SQL2000 it was possible only with standard SQL statements. Let’s create our first Stored Procedure.
Launch Visual Studio 2005 and open a new SQL Server Project. Add a new Stored Procedure to your project.
Using cnn As New SqlClient.SqlConnection("context connection=true")
cnn.Open()
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM Table_1", cnn)
SqlContext.Pipe.ExecuteAndSend(cmd)
End Using
Be carefull about our Conntection String. We do not define any specific connection string. Our stored procedure is already in SQL server, so we dont need to connect it again, we just use the default context connection.
Now Build your project and deploy it with "Build / Deploy" menu in VS2005.
Msg 6263, Level 16, State 1, Line 4
Execution of user code in the .NET Framework is disabled. Enable "clr enabled" configuration option.
This is the next error you will get. By default .NET Framwork is disabled in SQL2005. Explore "SQL Server 2005/Configuration Tools/Surface Area Configuration" in your Start menu. Select "Surface Area Configuration for Features", You will find "CLR Integration" option, activate it and save.
Done :) You can now just run your new stored procedure.
EXECUTE [dbo].[StoredProcedure1]
The first run will be a little slow; because CLR will compile your stored procedure but next runs will be more effective.