开发者

register C# assemblie and use functions in SQL Server

开发者 https://www.devze.com 2023-03-25 08:27 出处:网络
Suppose I have a static class in C#: public static class Math { [Microsoft.SqlServer.Server.SqlFunction]

Suppose I have a static class in C#:

public static class Math
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static int Add(int a, int b)
    {
        return a + b;
    }


    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void Void(int a, int b)
    {
    }

    static void Main(string[] args)
    {
    }

}

I know by @faester´s answer that to use this function in SQL we do:

 EXEC SP_CONFIGURE 'clr enabled', 1
    GO
    RECONFIGURE
    GO
    -- CONSIDER: DROP ASSEMBLY SqlClr
    GO
    CREATE ASSEMBLY SqlClr 
    FROM 'pathtoassembly'
    WITH PERMISSION_SET = SAFE;
    GO
    SELECT * FROM sys.assemblies
    GO
    CREATE FUNCTION [MathAdd] (@a int, @b int)
    RETURNS INT 
    AS EXTERNAL NAME [SqlClr].Math.[Add]
    GO 
    CREATE PROCEDURE [Void] @a INT, @b INT
    AS EXTERNAL NAME [SqlClr].Math.[Void]
    GO 
    SELECT dbo.MathAdd (1, 2)
    EXEC void 1, 2

To do this example fi开发者_如何学运维rst I added a main in the c# file

using System;
using System.Collections.Generic;
using System.Text;

public static class Math
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static int Add(int a, int b)
    {
        return a + b;
    }


    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void Void(int a, int b)
    {
    }

    static void Main(string[] args)
    {
    }

}

So, when I execute the SQL code... What path do I add. I was doing:

CREATE ASSEMBLY SqlClr 
    FROM 'C:\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe'
    WITH PERMISSION_SET = SAFE;
    GO

Obiously I must not add the .exe, so tried with the sln file and the cs file but can not get it to work...

I am getting the following, error:

Configuration option 'clr enabled' changed from 1 to 1. Run the RECONFIGURE statement to install. Msg 6501, Level 16, State 7, Line 1 CREATE ASSEMBLY failed because it could not open the physical file "C:\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe": 5(Acces denied.).

My question besides what to put in the path, is about how to build the c# code, do I only built it like any other program, do I add something else...

  • What is it mising?


I would suspect that it does't like the console app...

For a walkthrough see http://www.setfocus.com/technicalarticles/articles/clrfunctionforsqlserver.aspx

Some interesting resources see http://msdn.microsoft.com/en-us/library/w2kae45k.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消