Tip: Writing extended stored procedures using C #

zhaozj2021-02-16  71

What is an expansion stored procedure?

The extended stored procedure enables you to create your own external routines using programming languages ​​like C. For the user, the extended stored procedure is the same as the normal stored procedure, and the implementation method is also the same. The parameters can be passed to the extended stored procedure, and the extended stored procedure can return the result or return a state. The expansion stored procedure can be used to extend the functionality of Microsoft? SQL Server? 2000.

The extended stored procedure is a dynamic link library (DLL) that SQL Server can dynamically load and execute. The extended stored procedure runs directly on the address space of SQL Server and programs with SQL Server Open Data Services (ODS) API.

After writing the extended stored procedure, the member of the fixed server role sysadmin can register the extended stored procedure in SQL Server, and then granted other users to perform the permissions of the process. The extended stored procedure can only be added to the Master database.

Use C # to write extended stored procedures

Here's how to use a simple example to write the extended stored procedure with C #. First, we create a simple C-class library file:

// C # file: Csserver.cs using System; using System.Runtime.InteropServices; using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyTitle ( "CSServer")] [assembly: AssemblyDescription ( "Test SQL .NET interop ")] [assembly: AssemblyVersion (" 1.0.0.1 ")] [assembly: AssemblyDelaySign (false)] [assembly: AssemblyKeyFile (" myKey.snk ")] namespace SQLInterop {public interface ITest {string SayHello ();} [ ClassInterface (ClassInterFacetype.autodu)] Public class csharphelper: 特est {public string syhello () {return "hello from csharp";}}}

Then create Sn -k to create a strong name key file for this class, and compile. Sn -k mykey.snk CSC / T: library csserver.cs

Register this category: REGASM /TLB: CSSERVER.TLB CSSERVER.DLL / CODEBASE

转载请注明原文地址:https://www.9cbs.com/read-17607.html

New Post(0)