简体   繁体   English

在SQL Server中集成ac#类库

[英]Integration of a c# class library in SQL Server

I am trying to test the functionality of CLR Functions in SQL Server 2012. I found a tutorial online on how to basically do this and got it to work on my server.( https://www.skylinetechnologies.com/Blog/Skyline-Blog/March-2013/CLR-Functions-in-SQL-Server-A-Tutorial ) 我正在尝试在SQL Server 2012中测试CLR功能的功能。我在网上找到了有关如何基本执行此操作的教程,并将其在服务器上运行。( https://www.skylinetechnologies.com/Blog/Skyline-博客/ 2013年3月/ SQLR-Functions-in-SQL-Server-A-Tutorial

Now, i wanted to create a function that doesnt return a table but a string instead. 现在,我想创建一个不返回表而是返回字符串的函数。 In my understanding, the SQL Server needs some kind of object to work with, so i tried it with the following test method: 据我了解,SQL Server需要某种对象才能使用,因此我使用以下测试方法进行了尝试:

 public static class  TestSingleValue
{
  [SqlFunction(DataAccess = DataAccessKind.None, FillRowMethodName = "MyFillRowMethod", IsDeterministic = true)]

  public static SqlChars Test123()
    {
        SqlChars test = new SqlChars("teststring");
        return test;

    }

}

On the SQL server, i did the following: 在SQL Server上,我执行以下操作:

ALTER ASSEMBLY ClassLibrary2 from 'D:\SQL\TestCLR\ClassLibrary2.dll' with Permission_set = SAFE
CREATE FUNCTION TestCLR()
  returns nvarchar(max)
    AS 
       EXTERNAL name ClassLibrary2.[CLRTest.TestSingleValue].Test123
 GO 

Execute TestCLR

The SQL Server throws an error when executing the test method, saying that an "Object reference not set to an instance of an object" and further: SQL Server在执行测试方法时引发错误,指出“对象引用未设置为对象的实例”,并且进一步:

System.NullReferenceException: System.Data.SqlServer.Internal.ClrLevelContext.GetCurrentContextForLobAccess(>>CClrLobContext* pLobContext) System.Data.SqlServer.Internal.ClrLevelContext.GetXvarWlobStream(CXVariantBase pxvarSource, XvarLOBStreamInitCode eCode, Int64 lcid, SqlCompareOptions compareOpts, CClrLobContext pLobContext). System.NullReferenceException:System.Data.SqlServer.Internal.ClrLevelContext.GetCurrentContextForLobAccess(>> CClrLobContext * pLobContext)System.Data.SqlServer.Internal.ClrLevelContext.GetXvarWlobStream(CXVariantBase pxvarSource,XvarLOBStreamInitCode eCode,IntareClcobs,LintAccess,LintAccess,LintAccess,LintAccess,LintAccess,LintAccess

Can anyone tell me where i got the concept wrong or maybe provide a link to a good tutorial? 谁能告诉我我在哪里弄错了概念,或者可以提供指向好的教程的链接? I couldnt find one until now. 直到现在我都找不到。 Thanks in advance. 提前致谢。

Ok i found the answer myself, the problem is with "nvarchar(max)" as return type. 好的,我自己找到了答案,问题在于“ nvarchar(max)”作为返回类型。 You got to define a length to the nvarchar or use a workaround, then it works just fine. 您必须定义nvarchar的长度或使用变通方法,然后就可以正常工作。 Related: How to create CLR stored procedure with Nvarchar(max) parameter? 相关: 如何使用Nvarchar(max)参数创建CLR存储过程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM