T-SQL and CLR types for return value do not match

旧巷老猫 提交于 2019-12-23 12:28:44

问题


I'm trying to create a custom SQL Function by CLR Integration but I'm having a T-SQL and CLR types mismatch error.

DLL:

Imports System
Imports System.Data
Imports Microsoft.SqlServer.Server

Public Class Encrypter_Decrypter
    <SqlFunction(DataAccess:=DataAccessKind.Read)> _
    Public Shared Function EncryptString(ByVal strItem As String)
        Dim strPassPhrase As String = "***********"
        Dim strInitVector As String = "***********"
        Dim objEncryption = New PCI.Encryption()
        objEncryption.Initialise(strPassPhrase, strInitVector, -1, -1, -1, "", "", 1)
        Dim EncryptedString As String = objEncryption.Encrypt(strItem)
        objEncryption = Nothing
        EncryptString = EncryptedString
    End Function
End Class

Creating the SQL Function:

CREATE ASSEMBLY EncrypterDecrypter 
FROM 'c:\dll\Encrypter_Decrypter\Encrypter_Decrypter.dll'
WITH PERMISSION_SET = SAFE
GO

CREATE FUNCTION EncryptString(@strItem NVARCHAR(MAX)) RETURNS NVARCHAR(MAX) 
AS EXTERNAL NAME EncrypterDecrypter.Encrypter_Decrypter.EncryptString; 
GO

Error:

CREATE FUNCTION for "EncryptString" failed because T-SQL and CLR types for return value do not match.

Does anybody know what I'm missing?

Thanks :)


回答1:


You're missing an As String from the end of your function definition:

Public Shared Function EncryptString(ByVal strItem As String) As String

And obviously don't have Option Explicit or Option Strict turned on (either of which would alert you to this problem). By default, the return type of a function is Object, which doesn't have a mapping to any SQL Server data type.


Not knowing what your encryption package does, it's also usually a bad plan to be storing the encrypted version as a string (unless your package is automatically doing e.g. Base64 encoding?)




回答2:


Aren't you missing a return type for the function?



来源:https://stackoverflow.com/questions/7172326/t-sql-and-clr-types-for-return-value-do-not-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!