DataTypeInfos.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

要在集合中找到的项的索引。

返回

一个布尔值,指示是否可以通过索引访问集合。 true 值指示可以使用语法 DataTypeInfos[index] 访问集合。 false 值指示索引不能用于从 DataTypeInfos 集合中检索项。

示例

以下示例使用该方法 Contains 查看集合中的项数。

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

namespace DBProvInfos_GetEnum_Contains  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            //Create the DataTypeInfos collection.  
            DataTypeInfos dataInfos = new Application().DataTypeInfos;  

            //Create the enumerator.  
            DataTypeInfoEnumerator myEnumerator = dataInfos.GetEnumerator();  
            Console.WriteLine("The collection contains the following values:");  
            int i = 0;  
            DataTypeInfo dtiObject;  
            while ((myEnumerator.MoveNext()) && (myEnumerator.Current != null))  
            {  
                dtiObject = (DataTypeInfo)myEnumerator.Current;  
                Console.WriteLine("[{0}] {1} {2}", i++, dtiObject.TypeName, dtiObject.TypeEnumName);  
            }  
            // Reset puts the index pointer before the beginning.  
            // Do not retrieve from the collection until MoveNext is called.  
            myEnumerator.Reset();  
            myEnumerator.MoveNext();  

            // Now that the enumerator has been reset, use Contains.  
            //Boolean contains30 = dataInfos.Contains(30);  
            Console.WriteLine("contains at least 30 data types? {0}", dataInfos.Contains(30));  
            Console.WriteLine("contains at least 20 data types? {0}", dataInfos.Contains(20));  
            // Now that the enumerator has been reset, and moved to the  
            // first item in the collection, show the first item.  
            //dtiObject = (DataTypeInfo)myEnumerator.Current;  
            //Console.WriteLine("The first item in the enumerator after Reset:");  
            //Console.WriteLine("{0}, {1}", dtiObject.TypeName, dtiObject.TypeEnumName);  
            Console.WriteLine();  
        }  
    }  
}  
Imports System  
Imports System.Collections.Generic  
Imports System.Text  
Imports Microsoft.SqlServer.Dts.Runtime  

Namespace DBProvInfos_GetEnum_Contains  
    Class Program  
        Shared  Sub Main(ByVal args() As String)  
            'Create the DataTypeInfos collection.  
            Dim dataInfos As DataTypeInfos =  New Application().DataTypeInfos   

            'Create the enumerator.  
            Dim myEnumerator As DataTypeInfoEnumerator =  dataInfos.GetEnumerator()   
            Console.WriteLine("The collection contains the following values:")  
            Dim i As Integer =  0   
            Dim dtiObject As DataTypeInfo  
            While (myEnumerator.MoveNext()) &&(myEnumerator.Current <> Nothing)  
                dtiObject = CType(myEnumerator.Current, DataTypeInfo)  
                Console.WriteLine("[{0}] {1} {2}",i = Console.WriteLine("[{0}] {1} {2}",i + 1  
            End While  
            ' Reset puts the index pointer before the beginning.  
            ' Do not retrieve from the collection until MoveNext is called.  
            myEnumerator.Reset()  
            myEnumerator.MoveNext()  

            ' Now that the enumerator has been reset, use Contains.  
            'Boolean contains30 = dataInfos.Contains(30);  
            Console.WriteLine("contains at least 30 data types? {0}", dataInfos.Contains(30))  
            Console.WriteLine("contains at least 20 data types? {0}", dataInfos.Contains(20))  

            ' Now that the enumerator has been reset, and moved to the  
            ' first item in the collection, show the first item.  
            'dtiObject = (DataTypeInfo)myEnumerator.Current;  
            'Console.WriteLine("The first item in the enumerator after Reset:");  
            'Console.WriteLine("{0}, {1}", dtiObject.TypeName, dtiObject.TypeEnumName);  
            Console.WriteLine()  
        End Sub  
    End Class  
End Namespace  

示例输出:

该集合包含以下值:

[0] 浮点DT_R4

[1] 双精度浮点DT_R8

[2] 货币DT_CY

[3] 日期DT_DATE

[4] 布尔DT_BOOL

[5] 十进制DT_DECIMAL

[6] 单字节带符号整数DT_I1

[7] 单字节无符号整数DT_UI1

[8] 双字节带符号整数DT_I2

[9] 双字节无符号整数DT_UI2

[10] 四字节有符号整数DT_I4

[11] 四字节无符号整数DT_UI4

[12] 八字节带符号整数DT_I8

[13] 八字节无符号整数DT_UI8

[14] 文件时间戳DT_FILETIME

[15] 唯一标识符DT_GUID

[16] 字节流DT_BYTES

[17] 字符串DT_STR

[18] Unicode 字符串DT_WSTR

[19] 数值DT_NUMERIC

[20] 数据库日期DT_DBDATE

[21] 数据库时间DT_DBTIME

[22] 数据库时间戳DT_DBTIMESTAMP

[23] 图像DT_IMAGE

[24] 文本流DT_TEXT

[25] Unicode 文本流DT_NTEXT

包含至少 30 种数据类型? 错误

包含至少 20 种数据类型? True

适用于