在 .Net 中创建用户定义的 CLR 函数

发布于 12-14 03:31 字数 1114 浏览 5 评论 0原文

我想创建一个 CLR 函数,我创建了一个普通的类库文件并编码如下,我不想使用 SqlServerProject,因为我在那里找不到一些类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Types;

    namespace ClassLibrary1
    {
        public partial class Class1
        {
            [Microsoft.SqlServer.Server.SqlFunction]
            public static SqlString GetPassword(SqlString Value)
            {
                return Value;
            }
        }
    }

我编译了代码并从 sqlserver 创建了程序集,如下所示

CREATE ASSEMBLY ASSEM
authorization dbo
FROM 'E:\NBM Sites\tvt.stage.asentechdev1.com\ClassLibrary1.dll' WITH PERMISSION_SET = SAFE;

,并创建了一个函数,如下所示

CREATE FUNCTION SampleFunc
(   
    @value nvarchar(max)
)
RETURNS nvarchar(max) with execute as caller
AS
    External Name ASSEM.Class1.GetPassword
GO

,但上面的创建函数抛出了一个错误。

Could not find Type 'Class1' in assembly 'ClassLibrary1'.

我不明白,为什么 class1 不被认可,因为我也将其公开。 请任何人帮助我。

i want to create a CLR function, i created a normal class library file and coded as below, i dont want to use SqlServerProject, as i cannot find some classes there.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Types;

    namespace ClassLibrary1
    {
        public partial class Class1
        {
            [Microsoft.SqlServer.Server.SqlFunction]
            public static SqlString GetPassword(SqlString Value)
            {
                return Value;
            }
        }
    }

i compiled the code and created assembly from sqlserver like this

CREATE ASSEMBLY ASSEM
authorization dbo
FROM 'E:\NBM Sites\tvt.stage.asentechdev1.com\ClassLibrary1.dll' WITH PERMISSION_SET = SAFE;

and created a function as below

CREATE FUNCTION SampleFunc
(   
    @value nvarchar(max)
)
RETURNS nvarchar(max) with execute as caller
AS
    External Name ASSEM.Class1.GetPassword
GO

but the above create function threw an error saying.

Could not find Type 'Class1' in assembly 'ClassLibrary1'.

i dont understand, why the class1 is not recognised, as also i have made it public.
Please any one help me for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓2024-12-21 03:31:43

您缺少类所在的命名空间 (ClassLibrary1)。所以正确的create函数语句是:

CREATE FUNCTION SampleFunc
(   
  @value nvarchar(max)
)
RETURNS nvarchar(max) with execute as caller
AS
  External Name [ASSEM].[ClassLibrary1.Class1].[GetPassword]
GO

You are missing the namespace that your class is in (ClassLibrary1). So the correct create function statement is:

CREATE FUNCTION SampleFunc
(   
  @value nvarchar(max)
)
RETURNS nvarchar(max) with execute as caller
AS
  External Name [ASSEM].[ClassLibrary1.Class1].[GetPassword]
GO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文