SQL Server calls C #. NET write DLL, (share my HelloWorld quiz)

Source: Internet
Author: User
Tags command line
Server SQL Server calls C #. NET-Written DLL
1. Construct DLL: First we have to be in. NET inside Create a class library project, write a simple HelloWorld
Using System;

Namespace Mydll
{
<summary>
Summary description of the CLASS1.
</summary>
public class Class1
{
Public Class1 ()
{
//
TODO: Add constructor logic here
//
}
Public String SayHello ()
{
return "HelloWorld";
}
}
}
Mydll is the name of the DLL. After compiling, find MyDll.dll generated in your output directory, switch to command line mode, enter Sn-k mydll.snk generate key pair in MyDll.dll directory;
Then go back to your vs.net project and open AssemblyInfo.cs look [Assembly:assemblykeyfile (")] option, enter your key pair path '. \\.. \\bin\\Debug\\MyDll.snk '
Recompile MyDll.dll, finally assemble, enter at command line: regasm MyDll.dll, MyDll.dll copy to C:\WINDOWS\assembly directory after successful
(This step is heard, do not know what to use.) But actually I tried to call it without copying.

2,
SQL Server invocation Case:
DECLARE @ret int
DECLARE @object int
DECLARE @src varchar (500)
DECLARE @desc varchar (500)
DECLARE @return varchar (500)
exec @ret =sp_oacreate ' Mydll.class1 ', @object out
IF @ret <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src out, @desc out
SELECT Hr=convert (varbinary (4), @ret), source= @src, description= @desc
Return
End
--Call A is returns a value.
EXEC @ret = sp_OAMethod @object, ' SayHello ', @return out
IF @ret <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src out, @desc out
SELECT Ret=convert (varbinary (4), @ret), source= @src, description= @desc
Return
End
print ' result= ' + @return
--Destroy the object.
EXEC @ret = sp_OADestroy @object
IF @ret <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src out, @desc out
SELECT Ret=convert (varbinary (4), @ret), source= @src, description= @desc
Return
End





Related Article

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.