[Translation] Using CLR to call. NET methods in SQL Server

Source: Internet
Author: User
Original address: http://www.aspnetlibrary.com/articledetails.aspx? Article = Using-CLR-to-access-. NET-functions-in-SQL-Server

[Translation] Using CLR to call. NET methods in SQL Server

Original article released on: 2007.05.17
Author: Mark Smith
Translation: webabcd

Introduction
Let's take an example. Create a new class in. NET, create a new method in this class, and then call this method in SQL Server. According to Microsoft, SQL Server 2.0 significantly enhanced the database programming model through the host Microsoft. NET Framework 2005 Common Language Runtime Library (CLR. This allows developers to use any CLR language (such as C #, VB. NET or C ++) to write stored procedures, triggers, and user-defined functions.

How can we implement these functions?
To use CLR, perform the following steps:
1. Create a new class in. NET and create a public method in this class.
2. Compile this class as a DLL.
3. register the DLL in SQL Server.
4. Create an SQL Server Function to access the specified. NET method.

Next, let's complete an example.
First, create a new class library project named "SQLServerCLRTest" in Visual Studio. Create a new class named "CLRFunctions" and add a method named "HelloWold" to it. The Code is as follows: public class CLRFunctions
{
Public static string HelloWorld (string Name)
{
Return ("Hello" + Name );
}
}

This is a very simple method (in order for SQL Server to call it, it must be public and static), this method has a string type parameter, the returned message is "Hello" and the input parameter is added.

Now, we need to compile this project as a DLL and register it in SQL Server. This is also relatively simple. Right-click the project in VS and select "generate". Then the program will generate a DLL. If your project is in the debugging mode, you can find the compiled DLL in the path shown below. C: \ Documents ents and Settings \ mark. smith \ My Documents ents \ Visual Studio 2005 \ Projects \ SQLServerCLRTest \ bin \ Debug \ SQLServerCLRTest. dll

After finding this DLL, we can copy it to our SQL Server machine. If it is the same machine, we just need to remember this path.

Enable CLR
By default, the CLR in SQL Server is disabled, so we need to execute the following command to open CLR: exec sp_configure 'clr enabled', 1
Reconfigure
Go

Register DLL
To call the method we wrote, you need to register the DLL we just compiled in SQL Server. We can use the following command in the database to register the DLL (path to your DLL file) create assembly asmHelloWorld FROM 'C: \ SQLServerCLRTest. dll'

Call our. NET Method in SQL Server
To call the. NET method, we can write an SQL Server custom function and use "external name" in it to notify SQL Server to use the CLR function. The Code is as follows: create function dbo. clrHelloWorld
(
@ Name as nvarchar (200)
)
RETURNS nvarchar (200)
As external name asmHelloWorld. [SQLServerCLRTest. CLRFunctions]. HelloWorld

The preceding udfs do two tasks. First, an nvarchar parameter is declared, which is equivalent to the string type in. NET (if it is set to varchar and "external name" is used later, an error is returned ). Then, use "external name" to call the. NET method. Syntax: program set name. Class Name. Method Name

However, when I use this syntax to call.. NET method, SQL Server will report an error, so in order to make it work normally, I use the following syntax: Set Name. [Class Name]. method Name

Now we can use the following statement to call the. NET method: SELECT dbo. clrHelloWorld ('mark ')

When you run this code, you will get a returned result "Hello Mark ".

We use a simple example to demonstrate how to implement the SQL Server CLR, which can bring us a lot of useful help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.