SqlDataReader.GetValues(Object[]) 方法
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用当前行的列值来填充对象数组。
public:
override int GetValues(cli::array <System::Object ^> ^ values);
public:
virtual int GetValues(cli::array <System::Object ^> ^ values);
C#
public override int GetValues(object[] values);
C#
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
数组中 Object 的实例的数目。
以下示例演示如何使用大小正确的数组从提供的 SqlDataReader中的当前行读取所有值。 此外,该示例还演示了如何使用固定大小的数组,该数组可能小于或大于可用列数。
C#
private static void TestGetValues(SqlDataReader reader)
{
// Given a SqlDataReader, use the GetValues
// method to retrieve a full row of data.
// Test the GetValues method, passing in an array large
// enough for all the columns.
Object[] values = new Object[reader.FieldCount];
int fieldCount = reader.GetValues(values);
Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);
Console.WriteLine();
// Now repeat, using an array that may contain a different
// number of columns than the original data. This should work correctly,
// whether the size of the array is larger or smaller than
// the number of columns.
// Attempt to retrieve three columns of data.
values = new Object[3];
fieldCount = reader.GetValues(values);
Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);
}
Private Sub TestGetValues(ByVal reader As SqlDataReader)
' Given a SqlDataReader, use the GetValues
' method to retrieve a full row of data.
' Test the GetValues method, passing in an array large
' enough for all the columns.
Dim values(reader.FieldCount - 1) As Object
Dim fieldCount As Integer = reader.GetValues(values)
Console.WriteLine("reader.GetValues retrieved {0} columns.", _
fieldCount)
For i As Integer = 0 To fieldCount - 1
Console.WriteLine(values(i))
Next
Console.WriteLine()
' Now repeat, using an array that may contain a different
' number of columns than the original data. This should work correctly,
' whether the size of the array is larger or smaller than
' the number of columns.
' Attempt to retrieve three columns of data.
ReDim values(2)
fieldCount = reader.GetValues(values)
Console.WriteLine("reader.GetValues retrieved {0} columns.", _
fieldCount)
For i As Integer = 0 To fieldCount - 1
Console.WriteLine(values(i))
Next
End Sub
对于大多数应用程序,此方法提供了检索所有列的有效方法,而不是单独检索每个列。
可以传递包含 Object 少于结果行中包含的列数的数组。 仅将数组保留 Object 的数据量复制到数组。 还可以传递长度 Object 大于结果行中包含的列数的数组。
对于 null 数据库列,此方法返回 DBNull。
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided) |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |