MSSQL Stored Procedure calls C # DLL

Source: Internet
Author: User

 

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 !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.