Share via


DtsErrors.Item[Object] 属性

定义

从集合返回一个 DtsError 对象。

public:
 property Microsoft::SqlServer::Dts::Runtime::DtsError ^ default[System::Object ^] { Microsoft::SqlServer::Dts::Runtime::DtsError ^ get(System::Object ^ index); };
public Microsoft.SqlServer.Dts.Runtime.DtsError this[object index] { get; }
member this.Item(obj) : Microsoft.SqlServer.Dts.Runtime.DtsError
Default Public ReadOnly Property Item(index As Object) As DtsError

参数

index
Object

要返回的 DtsError 对象的错误号、索引或说明。

属性值

DtsError 对象。

示例

下面的代码示例创建一个“发送邮件”任务并将其添加到包中。 并非所有必需的发送邮件任务属性都已设置,因此当包运行时,集合中DtsErrorsDtsWarnings会出现错误和警告。 该方法 Contains 用于查看是否可以使用 DtsErrors[index] 语法访问集合,如果是这样,请使用该语法获取一些属性。

using System;  
using System.Collections.Generic;  
using System.Text;  
using Microsoft.SqlServer.Dts.Runtime;  
using Microsoft.SqlServer.Dts.Tasks.SendMailTask;  

namespace Microsoft.SqlServer.SSIS.Samples  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Package package = new Package();  
            TaskHost taskH2 = (TaskHost)package.Executables.Add("STOCK:SendMailTask");  
            taskH2.FailPackageOnFailure = false;  
            taskH2.FailParentOnFailure = false;  
            Console.WriteLine("SendMailTask: {0}", taskH2.ID);  

            package.MaximumErrorCount = 100;  
            package.FailPackageOnFailure = false;  
            package.FailParentOnFailure = false;  
            package.DelayValidation = true;  
            package.Execute();  

            // Get the collections.  
            DtsWarnings dtsWarns = package.Warnings;  
            DtsErrors dtsErrs = package.Errors;  
            // Use Contains to see if collection can be accessed  
            // using item syntax of [x].  
            Boolean warnItem = dtsWarns.Contains(0);  
            Boolean errItem = dtsErrs.Contains(0);  

            // If item sytax can be used, use it to obtain information.  
            if (warnItem)  
            {  
            //Using the Item method syntax of [x], obtain the first entry and a description.  
            DtsWarning firstWItem = dtsWarns[0];  
            String nameOfFirstItem = dtsWarns[0].SubComponent;  

            //Print the subcomponent for the warning located at position [0] two ways.  
            Console.WriteLine("The first warning subcomponent is: {0}", firstWItem.SubComponent);  
            Console.WriteLine("The first warning subcomponent is: {0}", nameOfFirstItem);  
            }  

            // If item sytax can be used, use it to obtain information.  
            if (errItem)  
            {  
            //Using the Item method syntax of [x], obtain the first entry and a description.  
            DtsError firstEItem = dtsErrs[0];  
            String nameOfFirstItem = dtsErrs[0].Description;  

            //Print the description of the warning located at position [0] two ways.  
            Console.WriteLine("The first error description is: {0}", firstEItem.Description);  
            Console.WriteLine("The first error description is: {0}", nameOfFirstItem);  
            }  
        }  
    }  
}  
Imports System  
Imports System.Collections.Generic  
Imports System.Text  
Imports Microsoft.SqlServer.Dts.Runtime  
Imports Microsoft.SqlServer.Dts.Tasks.SendMailTask  

Namespace Microsoft.SqlServer.SSIS.Samples  
    Class Program  
        static void Main(string() args)  
        {  
            Dim package As Package =  New Package()   
            Dim taskH2 As TaskHost = CType(package.Executables.Add("STOCK:SendMailTask"), TaskHost)  
            taskH2.FailPackageOnFailure = False  
            taskH2.FailParentOnFailure = False  
            Console.WriteLine("SendMailTask: {0}", taskH2.ID)  

            package.MaximumErrorCount = 100  
            package.FailPackageOnFailure = False  
            package.FailParentOnFailure = False  
            package.DelayValidation = True  
            package.Execute()  

            ' Get the collections.  
            Dim dtsWarns As DtsWarnings =  package.Warnings   
            Dim dtsErrs As DtsErrors =  package.Errors   
            ' Use Contains to see if collection can be accessed  
            ' using item syntax of [x].  
            Dim warnItem As Boolean =  dtsWarns.Contains(0)   
            Dim errItem As Boolean =  dtsErrs.Contains(0)   

            ' If item sytax can be used, use it to obtain information.  
            if (warnItem)  
            {  
            'Using the Item method syntax of [x], obtain the first entry and a description.  
            Dim firstWItem As DtsWarning =  dtsWarns(0)   
            Dim nameOfFirstItem As String =  dtsWarns(0).SubComponent   

            'Print the subcomponent for the warning located at position [0] two ways.  
            Console.WriteLine("The first warning subcomponent is: {0}", firstWItem.SubComponent)  
            Console.WriteLine("The first warning subcomponent is: {0}", nameOfFirstItem)  
            }  

            ' If item sytax can be used, use it to obtain information.  
            if (errItem)  
            {  
            'Using the Item method syntax of [x], obtain the first entry and a description.  
            Dim firstEItem As DtsError =  dtsErrs(0)   
            Dim nameOfFirstItem As String =  dtsErrs(0).Description   

            'Print the description of the warning located at position [0] two ways.  
            Console.WriteLine("The first error description is: {0}", firstEItem.Description)  
            Console.WriteLine("The first error description is: {0}", nameOfFirstItem)  
            }  
        }  
    End Class  
End Namespace  

示例输出:

SendMailTask: {12ADD307-23DA-42C9-A4B2-E360DEFD7563}

The first warning subcomponent is: Send Mail Task

The first warning subcomponent is: Send Mail Task

The first error description is: SMTP Server not specified

The first error description is: SMTP Server not specified

注解

如果对方法的 Contains 调用返回 true,则可以使用语法 Connections[index]访问集合中的指定元素。 Contains如果该方法返回false,此属性将引发异常。 在 C# 中,此属性是 DtsErrors 类的索引器。

适用于