OleDbDataReader.GetValues(Object[]) 方法

定义

使用当前行的列值来填充对象数组。Populates an array of objects with the column values of the current row.

public:
 override int GetValues(cli::array <System::Object ^> ^ values);
public:
 virtual int GetValues(cli::array <System::Object ^> ^ values);
public override int GetValues (object[] values);
public int GetValues (object[] values);
override this.GetValues : obj[] -> int
abstract member GetValues : obj[] -> int
override this.GetValues : obj[] -> int
Public Overrides Function GetValues (values As Object()) As Integer
Public Function GetValues (values As Object()) As Integer

参数

values
Object[]

要将属性列复制到的 Object 数组。An array of Object into which to copy the attribute columns.

返回

Int32

数组中 Object 的实例的数目。The number of instances of Object in the array.

实现

示例

using System;
using System.Data;
using System.Data.OleDb;

class Class1 {
   public static void Main() {
      using (OleDbConnection connection =
         new OleDbConnection("Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind")) {

         object[] meta = new object[10];
         bool read;

         OleDbCommand command = new OleDbCommand("select * from Region", connection);

         connection.Open();
         OleDbDataReader reader = command.ExecuteReader();

         if (reader.Read() == true) {
            do {
               int NumberOfColums = reader.GetValues(meta);

               for (int i = 0; i < NumberOfColums; i++)
                  Console.Write("{0} ", meta[i].ToString());

               Console.WriteLine();
               read = reader.Read();
            } while (read == true);
         }
         reader.Close();
      }
   }
}
Imports System.Data
Imports System.Data.OleDb

Module Module1
   Public Sub Main()
      Using connection As New OleDbConnection("Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind")
         Dim command As New OleDbCommand("select * from Region", connection)

         connection.Open()
         Dim reader As OleDbDataReader = command.ExecuteReader()

         Dim NumberOfColums As Integer
         Dim meta As Object() = New Object(10) {}
         Dim read As Boolean

         If reader.Read() = True Then
            Do
               NumberOfColums = reader.GetValues(meta)

               For i As Integer = 0 To NumberOfColums - 1
                  Console.Write("{0} ", meta(i).ToString())
               Next

               Console.WriteLine()
               read = reader.Read()
            Loop While read = True
         End If

         reader.Close()
      End Using
   End Sub
End Module

注解

对于大多数应用程序, GetValues 方法提供了一种有效的方法来检索所有列,而不是单独检索每个列。For most applications, the GetValues method provides an efficient means for retrieving all columns, instead of retrieving each column individually.

可以传递一个 Object 数组,该数组包含的列数少于所产生行中包含的列数。You can pass an Object array that contains fewer than the number of columns that are contained in the resulting row. 只有数组保存的数据量 Object 会复制到数组中。Only the amount of data the Object array holds is copied to the array. 还可以传递 Object 其长度大于结果行中包含的列数的数组。You can also pass an Object array whose length is more than the number of columns that are contained in the resulting row.

对于 null 数据库列,此方法返回 DBNullThis method returns DBNull for null database columns.

适用于