Share via


Variables.Item[Object] Propriété

Définition

Retourne un objet Variable à partir de la collection Variables.

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

Paramètres

index
Object

Nom, ID, description ou index de l'objet Variable à retourner.

Valeur de propriété

Objet Variable.

Exemples

L’exemple de code suivant ajoute une variable au package. L’exemple de code utilise différentes méthodes pour localiser la variable et imprimer son nom, sa valeur et son espace de noms.

using System;  
using System.Collections.Generic;  
using System.Text;  
using Microsoft.SqlServer.Dts.Runtime;  
namespace Adding_Variables  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Application app = new Application();  
            // Load a sample package that contains a variable that sets the file name.  
            Package pkg = app.LoadPackage(@"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx", null);  
            Variables pkgVars = pkg.Variables;  
            Variable myVar = pkg.Variables.Add("myCustomVar", false, "User", "3");  

            // Verify whether the variable is in the collection now.  
            Boolean hasMyVar = pkg.Variables.Contains("myCustomVar");  
            Console.WriteLine("The variable was found? {0}", hasMyVar);  

            // Loop over the collection using the foreach keyword.  
            foreach (Variable pkgVar in pkgVars)  
            {  
                // Print variables only from the User namespace.  
                if (pkgVar.Namespace == "User")  
                {  
                Console.WriteLine("Variable: {0}, {1}", pkgVar.Name, pkgVar.Value.ToString());  
                 }  
            }  
            Console.WriteLine("---------------------------");  
            // Loop over the collection using the Enumerator.   
            VariableEnumerator myEnum = pkg.Variables.GetEnumerator();  
            int i = 0;  
            while ((myEnum.MoveNext()) && (myEnum.Current != null))  
                // Again, show only the variables in the User namespace.  
                if (myEnum.Current.Namespace == "User")  
                {                  
                    Console.WriteLine("[{0}] {1}, {2}", i++, myEnum.Current.Name, myEnum.Current.Namespace);  
                }  

            myEnum.Reset();  
            Console.WriteLine("---------------------------");  

            //Using the Item method syntax of [x], obtain the  
            // first entry in the collection.  
            myVar = pkgVars[0];  
            Console.WriteLine("The name and namespace of the first variable is: {0}, {1}", myVar.Name, myVar.Namespace);  
            String nameOfFirstItem = pkgVars[0].Name;  
            Console.WriteLine("The name of the first variable is: {0}", nameOfFirstItem);  
            //}  
        }  
    }  
}  
Imports System  
Imports System.Collections.Generic  
Imports System.Text  
Imports Microsoft.SqlServer.Dts.Runtime  
Namespace Adding_Variables  
    Class Program  
        Shared  Sub Main(ByVal args() As String)  
            Dim app As Application =  New Application()   
            ' Load a sample package that contains a variable that sets the file name.  
            Dim pkg As Package =  app.LoadPackage("C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx",Nothing)   
            Dim pkgVars As Variables =  pkg.Variables   
            Dim myVar As Variable =  pkg.Variables.Add("myCustomVar",False,"User","3")   

            ' Verify whether the variable is in the collection now.  
            Dim hasMyVar As Boolean =  pkg.Variables.Contains("myCustomVar")   
            Console.WriteLine("The variable was found? {0}", hasMyVar)  

            ' Loop over the collection using the foreach keyword.  
            Dim pkgVar As Variable  
            For Each pkgVar In pkgVars  
                ' Print variables only from the User namespace.  
                If pkgVar.Namespace = "User" Then  
                Console.WriteLine("Variable: {0}, {1}", pkgVar.Name, pkgVar.Value.ToString())  
                End If  
            Next  
            Console.WriteLine("---------------------------")  
            ' Loop over the collection using the Enumerator.   
            Dim myEnum As VariableEnumerator =  pkg.Variables.GetEnumerator()   
            Dim i As Integer =  0   
            While (myEnum.MoveNext()) &&(myEnum.Current <> Nothing)  
                    Console.WriteLine("[{0}] {1}, {2}",i = Console.WriteLine("[{0}] {1}, {2}",i + 1  
            End While  

            myEnum.Reset()  
            Console.WriteLine("---------------------------")  

            'Using the Item method syntax of [x], obtain the  
            ' first entry in the collection.  
            myVar = pkgVars(0)  
            Console.WriteLine("The name and namespace of the first variable is: {0}, {1}", myVar.Name, myVar.Namespace)  
            Dim nameOfFirstItem As String =  pkgVars(0).Name   
            Console.WriteLine("The name of the first variable is: {0}", nameOfFirstItem)  
            '}  
        End Sub  
    End Class  
End Namespace  

Exemple de sortie :

La variable a été trouvée ? True

Variable : myCustomVar, 3

---------------------------

[0] myCustomVar, Utilisateur

---------------------------

Le nom et l’espace de noms de la première variable sont : CancelEvent, System

Le nom de la première variable est : CancelEvent

Remarques

Si l’appel à la Contains méthode retourne true, vous pouvez accéder à l’élément spécifié dans la collection à l’aide de la syntaxe Variables[index]. Si la Contains méthode retourne false, cette propriété lève une exception. En C#, cette propriété est l'indexeur correspondant à la classe Variables.

S’applique à