I created a simple DLL in C# (using Visual Studio 2017) and I'm trying to call it from a stored procedure in Sybase 17. When I call the Store Procedures, I received an error message saying that method could not be found in the DLL. It looks like the methods inside the DLL is not visible outside?! Sybase Procedures: CREATE PROCEDURE "mystring"() External name 'MyMethodTEst@c:\\D\\AmosTest.dll' language "C_ESQL64" Call the store Procedure: CALL "amos"."mystring"() Sybase Error: -- Could not execute statement. -- Procedure 'mystring' terminated with unhandled exception 'Could not find --'MyMethodTEst' in dynamic library 'c:\D\AmosTest.dll'' -- SQLCODE=-91, ODBC 3 State="HY000" -- (Continuing after error) -- Procedure completed DLL Source code: using System; using System.Runtime.InteropServices; namespace AmosTest { [ComVisible(true)] [ProgId("AmosTest.Class1")] public class Class1 { public string MyMethodTEst(string param) { return "OK!!" ; } } } |
The functions have to be declared static and your external procedure definition is declared incorrectly as it is not being supplied a parameter ( and should really be a function since you are returning a value). That said, the key issue for the error is that the method is AmosTest.Class1.MyMethodTEst (the namespace, class, and method). The correct C# code is: using System.Runtime.InteropServices; namespace AmosTest { [ComVisible(true)] [ProgId("AmosTest.Class1")] public class Class1 { public static string MyMethodTEst(string param) { return "OK!!" ; } } } And the function in SQL Anywhere should be: create or replace function mystring( in c long varchar ) returns long varchar external name 'AmosTest.dll::AmosTest.Class1.MyMethodTEst(string) string' language clr; And called like: select mystring('Hi!'); |
Your CREATE PROCEDURE statement would fit for a C/C++ external function, not for a CLR method. For native CLR functions and procedures you need to specify "LANGUAGE CLR", and the class name must be added, as well. Confine the sample from the docs.
Thanks a lot for you feedback.
I Have modified the Store Procedure:
ALTER PROCEDURE mystring() external name 'Class1.MyMethodTEst@c:\D\AmosTest.dll' language CLR
BUT I've got this error:
CALL mystring()
-- Could not execute statement. -- Procedure 'mystring' terminated with unhandled exception 'Invalid method -- signature: 5AmosTest.Class1.MyMethodTEst@c:\D\AmosTest.dll' -- SQLCODE=-91, ODBC 3 State="HY000" -- (Continuing after error) -- Procedure completed
Any Idea? Thanks again in advances. Ale
Can't tell whether the path/location is correct, however, AFAIK all CRL methods must be declared static when called from SQL Anywhere, cf. the samples.
In other words: I'd recommend to try to run a given sample - and once that does work, try my own approaches...
Hi Volker
First of all thanks again, now I fixed, the problem was the .NET project. I create a new project in c# and not in C++ and now when I Compile the DLL it works fine. Thanks again for your help!