In a recent project, you need to call the DLL in the stored procedure, find some information, and there are many implementation methods. The following is my practice.
1. Use VS to create a class library project named myDll
Namespace myDll
{
Public class myClass
{
Public string myFunction (string str)
{
Return str;
}
}
}
2. Set the type in the Assembly to visible to the COM Component
In the AssemblyInfo. cs file, find [assembly: ComVisible (false)] and change false to true.
3. Register DLL
Enter regasm/codebase Dllpath In the. net command line.
Iv. Stored Procedure Call DLL
Create PROC P_testDLL (
@ Str VARCHAR (100)
)
AS
DECLARE @ objectID INT
DECLARE @ Functionreturn varchar (8000)
SET NOCOUNT ON
EXEC sp_OACreate 'mydll. myclass', @ objectID OUT
EXEC sp_OAMethod @ objectID, 'myfunction', @ Functionreturn OUT, @ str
Print @ Functionreturn
EXEC sp_OADestroy @ objectID
SET NOCOUNT OFF
RETURN
I don't know if there is any better way to share it !!!