PrecedenceConstraints.Contains(Object) 方法

定义

指示是否可以通过使用索引访问集合中的项而不引发异常。

public:
 bool Contains(System::Object ^ index);
public bool Contains (object index);
member this.Contains : obj -> bool
Public Function Contains (index As Object) As Boolean

参数

index
Object

要在集合中找到的名称、标识、ID 或索引。

返回

Boolean

一个布尔值,指示是否可以按名称、标识、ID 或索引访问集合。 true 值指示可以使用语法 PrecedenceConstraints[index] 访问集合。 false 值指示索引不能用于从 PrecedenceConstraints 集合中检索项;使用此属性引发异常。

示例

下面的代码示例创建两个优先约束。 然后,它用于Contains验证是否存在特定约束、计算方法使用前后Remove的约束数,并演示如何使用GetEnumerator循环访问集合,使用Item[]返回约束的语法pConsts[i].IDID

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

namespace PrecedenceConst  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Package pkg = new Package();  
            // Add a File System task.  
            Executable eFileTask1 = pkg.Executables.Add("STOCK:FileSystemTask");  
            TaskHost thFileTask1 = eFileTask1 as TaskHost;  

            // Add a second File System task.  
            Executable eFileTask2 = pkg.Executables.Add("STOCK:FileSystemTask");  
            TaskHost thFileTask2 = eFileTask2 as TaskHost;  

            // Add a Bulk Insert task.  
            Executable eBulkInsert = pkg.Executables.Add("STOCK:BulkInsertTask");  
            TaskHost thBulkInsert = eBulkInsert as TaskHost;  

            // Add a precedence constraint between eFileTask1 and eFileTask2.  
            // Set the constraint to be that eFileTask2 cannot run   
            // until eFileTask1 completes.  
            PrecedenceConstraint pcFileTasks = pkg.PrecedenceConstraints.Add(eFileTask1, eFileTask2);  
            pcFileTasks.Name = "constraint between File System Tasks";  

            // Add another precedence constraint. Add it between eFileTask2 and BulkInsert.  
            // Again, set the constraint to be that BulkInsert cannot run   
            // until eFileTask2 completes.  
            PrecedenceConstraint pcFiletoBulk = pkg.PrecedenceConstraints.Add(eFileTask2, eBulkInsert);  
            pcFileTasks.Name = "constraint between File System and Bulk Insert Tasks";  

            // Obtain the precedence constraint collection.  
            PrecedenceConstraints pConsts = pkg.PrecedenceConstraints;  
            Boolean containsConstraint = pkg.PrecedenceConstraints.Contains("constraint between File System and Bulk Insert Tasks");  
            Console.WriteLine("Contains the constraint between File System and Bulk Insert Tasks? {0}", containsConstraint);  

            // Create the enumerator.  
            PrecedenceConstraintEnumerator myEnumerator = pConsts.GetEnumerator();  
            // Iterate over the collection, and remove the Bulk Insert task.  
            int i = 0;  
            while ((myEnumerator.MoveNext()) && (myEnumerator.Current != null))  
            {  
                Console.WriteLine("ID {0}", pConsts[i].ID);  
            }  

            Console.WriteLine("Number of constraints {0}", pConsts.Count);  
            // Remove the second contstraint.  
            pConsts.Remove(1);  
            // Verify that one has been removed.  
            Console.WriteLine("Number of constraints {0}", pConsts.Count);  

            Console.WriteLine();  
        }  
    }  
}  
Imports System  
Imports System.Collections.Generic  
Imports System.Text  
Imports Microsoft.SqlServer.Dts.Runtime  
Imports Microsoft.SqlServer.Dts.Tasks.FileSystemTask  
Imports Microsoft.SqlServer.Dts.Tasks.BulkInsertTask  

Namespace PrecedenceConst  
    Class Program  
        Shared  Sub Main(ByVal args() As String)  
            Dim pkg As Package =  New Package()   
            ' Add a File System task.  
            Dim eFileTask1 As Executable =  pkg.Executables.Add("STOCK:FileSystemTask")   
            Dim thFileTask1 As TaskHost =  eFileTask1 as TaskHost   

            ' Add a second File System task.  
            Dim eFileTask2 As Executable =  pkg.Executables.Add("STOCK:FileSystemTask")   
            Dim thFileTask2 As TaskHost =  eFileTask2 as TaskHost   

            ' Add a Bulk Insert task.  
            Dim eBulkInsert As Executable =  pkg.Executables.Add("STOCK:BulkInsertTask")   
            Dim thBulkInsert As TaskHost =  eBulkInsert as TaskHost   

            ' Add a precedence constraint between eFileTask1 and eFileTask2.  
            ' Set the constraint to be that eFileTask2 cannot run   
            ' until eFileTask1 completes.  
            Dim pcFileTasks As PrecedenceConstraint =  pkg.PrecedenceConstraints.Add(eFileTask1,eFileTask2)   
            pcFileTasks.Name = "constraint between File System Tasks"  

            ' Add another precedence constraint. Add it between eFileTask2 and BulkInsert.  
            ' Again, set the constraint to be that BulkInsert cannot run   
            ' until eFileTask2 completes.  
            Dim pcFiletoBulk As PrecedenceConstraint =  pkg.PrecedenceConstraints.Add(eFileTask2,eBulkInsert)   
            pcFileTasks.Name = "constraint between File System and Bulk Insert Tasks"  

            ' Obtain the precedence constraint collection.  
            Dim pConsts As PrecedenceConstraints =  pkg.PrecedenceConstraints   
            Dim containsConstraint As Boolean =  pkg.PrecedenceConstraints.Contains("constraint between File System and Bulk Insert Tasks")   
            Console.WriteLine("Contains the constraint between File System and Bulk Insert Tasks? {0}", containsConstraint)  

            ' Create the enumerator.  
            Dim myEnumerator As PrecedenceConstraintEnumerator =  pConsts.GetEnumerator()   
            ' Iterate over the collection, and remove the Bulk Insert task.  
            Dim i As Integer =  0   
            While (myEnumerator.MoveNext()) &&(myEnumerator.Current <> Nothing)  
                Console.WriteLine("ID {0}", pConsts(i).ID)  
            End While  

            Console.WriteLine("Number of constraints {0}", pConsts.Count)  
            ' Remove the second contstraint.  
            pConsts.Remove(1)  
            ' Verify that one has been removed.  
            Console.WriteLine("Number of constraints {0}", pConsts.Count)  

            Console.WriteLine()  
        End Sub  
    End Class  
End Namespace  

示例输出:

包含文件系统和大容量插入任务之间的约束? True

ID {BA42AF32-3AC1-4CB6-85A2-289760ADBFA4}

ID {BA42AF32-3AC1-4CB6-85A2-289760ADBFA4}

约束数 2

约束数 1

注解

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

适用于