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 :)
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?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With