I've been looking into the programmable aspects of SQL Server 2005 for the past days and one of the things that interest me most is the CLR Integration. Developers can now use managed-code to develop stored procedures that can be accessed by SQL Server 2005.
Here's a simple example I made:
Step 1 - Creating the assembly.
1. Create a Library project using VS.NET 2005
2. Enter the following code:
using System;
using System.Data;
using System.Data.SqlServer;
public class TestClass
{
public static void RunTest()
{
SqlCommand cm = SqlContext.GetCommand();
cm.CommandText = "SELECT * FROM Production.Product";
SqlDataReader dr = cm.ExecuteReader();
SqlContext.GetPipe().Send(dr);
}
}
3. Compile the assembly.
Noticed the namespace System.Data.SqlServer and the new class SqlContext? These are new stuffs. The GetPipe() method actually returns a SqlPipe object that allows you to send results to SQL Server 2005.
Step 2 - Loading the assembly in SQL Server 2005
1. From SQL Server Management Studio, Create a New Query.
2. Type in the following and run it:
CREATE ASSEMBLY Test FROM 'D:\test\SQLCLRTest.dll'
WITH PERMISSION_SET=UNSAFE
The path following the FROM statement should point to where your assembly is located. If there are no errors, you have successfully loaded the assembly.
3. Next, type and run this:
CREATE PROCEDURE RunTest
AS EXTERNAL NAME Test.TestClass.RunTest
This will create a Stored Procedure for you. You can even see it in your Stored Procedures list.
4. Finally, to run your procedure, type and run the following:
EXEC RunTest
You should get some results.
Step 3 - To remove the procedure and unload the assembly after you are fed up with it:
1) Run DROP PROCEDURE RunTest
2) Then run DROP ASSEMBLY Test
Have Fun!
Home »Unlabelled » SQL.2005.CLR.Integration
Popular Post
-
H ave been trying to download Office 2007 Beta 1 Refresh for the past few days but the line traffic condition was so bad. With just below 10...
-
Check out new Tiger Baby single Landscapes ...taken from the band's new album Open Windows Open Hills . Predictably, this is catc...
-
Is it Hunting Season again? Everyone I know seems to be looking (or have looked) for greener pastures nowadays. I was so surprised to find ...
-
M y Streamyx was down for approximately five days. The technician told me that it is a problem with my port over in their headquarters. Acco...
-
The Uri (Namibia) Uri. From the Namibian word for "jump", this extremely able 4x4 is perfect for the real off road path. Made in N...
-
T eam Foundation Server has finally shipped! This completes the Visual Studio Team System family. Incase you are unaware, Team Foundation S...
-
Been working on a new blog...in Danish. Not really sure why, but I probably just need some fresh air or something. Check it out if you like...
-
S QL Server 2005 SP1 March 2006 CTP and its accompanying SQL Server 2005 Books Online (March 2006 CTP) are now available for preview. User...
No comments:
Post a Comment