Skip to main content
 首页 » 编程设计

c#之如何从 SSIS 脚本任务将值传递给 Web 服务

2024年02月06日32rubylouvre

我正在关注这个 blog将值写入 Web 服务。到目前为止,我已经成功地读取了我的数据集并将其存储在一个对象变量中,然后循环遍历它们以一个接一个地显示它。

    Imports System 
    Imports System.Data 
    Imports System.Math 
    Imports Microsoft.SqlServer.Dts.Runtime 
    Imports System.Xml 
    Imports System.Data.OleDb 
 
    Public Class ScriptMain 
 
     Public Sub Main() 
 
      Dim oleDA As New OleDbDataAdapter 
      Dim dt As New DataTable 
      Dim col As DataColumn 
      Dim row As DataRow 
      Dim sMsg As String 
 
      oleDA.Fill(dt, Dts.Variables("dsVar").Value) 
 
      For Each row In dt.Rows 
       For Each col In dt.Columns 
        sMsg = sMsg & col.ColumnName & ": " & _ 
               row(col.Ordinal).ToString & vbCrLf 
       Next 
       MsgBox(sMsg)   //These 2 lines need to be changed 
       sMsg = ""      //So that the Results can be sent to the web service. 
      Next 
 
      Dts.TaskResult = Dts.Results.Success 
 
     End Sub 
 
    End Class 

这一切都很好。

由于我没有任何 .net 编程经验,我希望在更改代码以将相同的值写入 Web 服务调用方面找到一些帮助。我需要将几行和几列发送到 Web 服务,后者又会将数据写入另一个数据库(它的 DMZ 和其他东西,我对此不太了解)。有人能指出我正确的方向吗?我不打算使用 Web 服务任务,因为我的老板告诉我他在使用它时已经遇到了问题。

非常感谢这方面的任何帮助。

请您参考如下方法:

您可以做的是添加一个脚本任务来调用 Web 服务,将数据集中的值作为请求变量传递 - 您可以通过单击程序包资源管理器然后单击变量在 SSIS 程序包中添加请求和响应变量。在您的响应变量的值中,例如,对于您可以拥有的 value 属性:

<?xml version="1.0" encoding="utf-8"?> <GetUpdatedResponse  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">   <yourDataset/></GetUpdatedResponse> 

这是您响应的 XML。

设置变量后,您可以指定要传递到脚本任务中的变量,例如用户::你的请求变量

然后您可以创建一个脚本任务,如下所示:

/// <summary> 
/// This method is called when this script task executes in the control flow. 
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or 
/// failure. 
/// </summary> 
    public void Main() 
    { 
        if (Dts.Variables.Contains("yourReqVar") == true) 
        { 
            try 
            { 
                object nativeObject = Dts.Connections["YourWebservice"].AcquireConnection(null); 
                HttpClientConnection conn = new HttpClientConnection(nativeObject); 
 
                YourService ws = new YourService(conn.ServerURL); 
                GetUpdatedRequest req = new GetUpdatedRequest(); 
                req.username = conn.ServerUserName; 
                req.password = "A123232"; 
                req.dateRange = new dateRange(); 
                req.dateRange.from = DateTime.Now.AddDays((dayIncrement * -1)); 
                req.dateRange.to = DateTime.Now; 
                req.dateRange.fromSpecified = true; 
                req.dateRange.toSpecified = true; 
                GetUpdatedResponse response = ws.GetUpdated(req); 
 
                System.Xml.Serialization.XmlSerializer x 
                  = new System.Xml.Serialization.XmlSerializer(response.GetType()); 
                StringWriterWithEncoding responseToXml 
                  = new StringWriterWithEncoding(new StringBuilder(), Encoding.UTF8); 
 
                x.Serialize(responseToXml, response); 
                Dts.Variables["User::GetUpdated"].Value = responseToXml.ToString(); 
                Dts.TaskResult = (int)ScriptResults.Success; 
            } 
            catch (Exception) { 
                Dts.Events.FireWarning(0, "Skip", "Failed to retrieve updated.", String.Empty, 0); 
                Dts.TaskResult = (int)ScriptResults.Success; 
            } 
        } 
        else 
        { 
            Dts.TaskResult = (int)ScriptResults.Failure; 
        } 
    }