客观的:
我们想使用执行 HTTP 请求的 .Net 3.5 使用 VB.NET 2008 编写一个 DLL
然后,我们要从 SQL Server 2005(在触发器中)调用 DLL,并向 DLL 传递一些变量并获得返回值。
我在这个过程中的位置。
我已经创建了 DLL 'HTTPDLL.dll'
资源:
Imports System
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.Security
Imports System.Security.Permissions
Imports System.IO
Imports Microsoft.SqlServer.Server
Public Class HTTPDLL
Public Function HTTPPost(ByVal URL As String, ByVal Content As String) As Boolean
Dim myURL As String = Trim(URL)
Dim mycontent As String = Trim(Content)
Dim rawOutput As String = ""
'Get Acces Right to web
Dim p As New WebPermission(NetworkAccess.Connect, myURL)
p.Assert()
'Prepare the web Request
Dim req As WebRequest = WebRequest.Create(myURL)
req.Timeout = 60000
req.Method = "POST"
req.ContentLength = mycontent.Length
req.ContentType = "application/x-www-form-urlencoded"
Dim sw As New StreamWriter(req.GetRequestStream())
sw.Write(mycontent)
sw.Close()
Dim resp As WebResponse = req.GetResponse()
Dim sr As New StreamReader(resp.GetResponseStream)
rawOutput = sr.ReadToEnd()
sr.Close()
Return True
End Function
Public Function Test() As Integer
Return 1
End Function
Public Function DllMain(ByVal hInst As Long, ByVal fdwReason As Long, _
ByVal lpvReserved As Long) As Boolean
Dim s As String = "sdfghjkolihdgjkhgiueghkyhekygigdjhgfkhsgdkhfgksjdhgkjshdgfkjhsgdkjfhgkshdgfkjhgskjdhgfkjhsdgkfhgskjdhfgkjsdhgfkshdgfkhgsdkfhgksdhfgkshdgfkshdgfkjhsgdkfhgskdhfgksjdhgfkjshgdfkhsgdkfhgskdhfgkshgdfkjhgskdjg"
s &= "kjhdgjkshkdjfhklsjdhfljhgkhvbfiuvbli klrfgliu ghliebliuhdflivbdkljhgljuhfvliuhdf"
Return True
End Function
End Class
我已将 DLL 放在 SQL Server c:/Windows/System 文件夹中
我已经加载了这些程序集
HelloWorld System.Core System.XML.Linq
使用这些命令
EXEC sp_configure 'show advanced options' , '1';
go
reconfigure
go
EXEC sp_configure 'clr enabled' , '1'
go
sp_configure 'Ole Automation Procedures', '1'
GO
RECONFIGURE
-- Turn advanced options back off
EXEC sp_configure 'show advanced options' , '0';
go
reconfigure
GO
-- assembly style
ALTER DATABASE master SET TRUSTWORTHY ON
GO
CREATE ASSEMBLY [System.Core]
AUTHORIZATION [dbo]
FROM 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll'
WITH PERMISSION_SET = UNSAFE
GO
CREATE ASSEMBLY [System.Xml.Linq]
AUTHORIZATION [dbo]
FROM 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll'
WITH PERMISSION_SET = UNSAFE
GO
create assembly HelloWorld from 'C:\WINDOWS\system\HTTPDLL.dll'
with permission_set = safe
GO
然后我添加了扩展程序
USE [master]
GO
/****** Object: ExtendedStoredProcedure [dbo].[HTTPDLL] Script Date: 02/03/2010 11:45:28 ******/
EXEC dbo.sp_addextendedproc N'HTTPDLL', 'C:\WINDOWS\system\HTTPDLL.dll'
(此时我不确定为什么或是否需要添加程序集,因为它直接引用了 Dll。)
我需要解决的问题。我现在想调用 DLL 的(扩展过程)HTTPPost 函数
我尝试过的 exec HTTPPost "Msg 17750, Level 16, State 0, Procedure HTTPPost, Line 1 无法加载 DLL HelloWorld 或其引用的 DLL 之一。原因:126(找不到指定的模块。)。 "
exec HelloWorld.HTTPPost 'http://www.somewebsite.com',''
“消息 2812,级别 16,状态 62,第 1 行找不到存储过程 'HelloWorld.HTTPPost'。”
我需要什么:如果您可以查看我的实施,看看我是否正确设置了所有内容,并告诉我(请提供示例)如何正确调用该 DLL,向其发送变量并捕获其返回值。我将不胜感激。
我浏览了许多帖子/文章,并得到了一些让我走到这一步的点点滴滴,但没有什么是包罗万象的。
我也试过这个
-- Scratch variables used in the script
DECLARE @retVal INT
DECLARE @comHandle INT
DECLARE @errorSource VARCHAR(8000)
DECLARE @errorDescription VARCHAR(8000)
DECLARE @retString VARCHAR(100)
-- Initialize the COM component.
EXEC @retVal = sp_OACreate 'HTTPDLL.HTTPDLL', @comHandle OUTPUT
IF (@retVal <> 0)
BEGIN
-- Trap errors if any
EXEC sp_OAGetErrorInfo @comHandle, @errorSource OUTPUT, @errorDescription OUTPUT
SELECT [Error Source] = @errorSource, [Description] = @errorDescription
RETURN
END
(和其他此类变体)并在 sp_OAGetErrorInfo 的返回中获取此信息
“错误源描述 ODSOLE 扩展过程无效的类字符串”
也许我调用不正确,但我无法让它工作(总是返回'无效的类字符串')
预先感谢您的帮助!。