[英]register C# assemblie and use functions in SQL Server
Suppose I have a static class in C#: 假设我在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: 通过@faester的答案 ,我知道要在SQL中使用此功能,我们可以这样做:
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 首先要执行此示例, 我在c#文件中添加了main
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. 因此,当我执行SQL代码时...我要添加什么路径。 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... 令人讨厌的是,我绝对不能添加.exe ,因此尝试使用sln文件和cs文件,但无法使其正常工作...
I am getting the following, error: 我收到以下错误消息:
Configuration option 'clr enabled' changed from 1 to 1. Run the RECONFIGURE statement to install.
配置选项“ clr enabled”从1更改为1。运行RECONFIGURE语句进行安装。 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.).
消息6501,级别16,状态7,行1创建装配失败,因为它无法打开物理文件“ C:\\ Visual Studio 2005 \\ Projects \\ ConsoleApplication1 \\ ConsoleApplication1 \\ bin \\ Debug \\ ConsoleApplication1.exe”:5(拒绝访问。 )。
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... 我的问题除了要放在路径中的内容外 ,还有关于如何构建c#代码的问题,我是否仅像其他任何程序一样构建它,是否还要添加其他内容...
I would suspect that it does't like the console app... 我怀疑它不喜欢控制台应用程序...
For a walkthrough see http://www.setfocus.com/technicalarticles/articles/clrfunctionforsqlserver.aspx 有关演练,请参见http://www.setfocus.com/technicalarticles/articles/clrfunctionforsqlserver.aspx
Some interesting resources see http://msdn.microsoft.com/en-us/library/w2kae45k.aspx 一些有趣的资源请参见http://msdn.microsoft.com/en-us/library/w2kae45k.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.