0

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 first 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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.