sqldatareader GetValues怎么用
3个回答
展开全部
下列范例示范如何使用适当大小的阵列,从提供之 SqlDataReader 中目前的资料列读取所有值。此外,此范例示范了使用固定大小之阵列的情形,而该阵列可能小於或大於可用资料行的数目。
privatestaticvoid 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]);
}
privatestaticvoid 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]);
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲、导入文档内容”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表...
点击进入详情页
本回答由AiPPT提供
展开全部
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]);
}
{
// 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]);
}
参考资料: http://technet.microsoft.com/zh-tw/library/system.data.sqlclient.sqldatareader.getvalues.aspx
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参数 :要复制的Object 数组
返回值:数组中 Object 的实例的数目。
使用时两点要注意:1,使用完后,必须调用Close 方法. 2,数组中的值 和 SQL 语句中 SELECT 的列顺序一样,比如 SELECT A,B,C FROM TABLE 那么返回值的数据 objs[0] 就是A 的值,objs[1] 就是B 的值,以此类推.
SqlCommand cmd = new SqlCommand("SELECT A,B,C FROM TABLE",cnn);
SqlDataReader reader = cmd.ExecuteReader();
if (!reader.HasRow())
{
reader.Close();
return null;
}
while (reader.Read())
{
Object[] objs = new Object[reader.FieldCount];
reader.GetValues(objs);
//下面应该是你处理数据的代码
}
reader.close();
返回值:数组中 Object 的实例的数目。
使用时两点要注意:1,使用完后,必须调用Close 方法. 2,数组中的值 和 SQL 语句中 SELECT 的列顺序一样,比如 SELECT A,B,C FROM TABLE 那么返回值的数据 objs[0] 就是A 的值,objs[1] 就是B 的值,以此类推.
SqlCommand cmd = new SqlCommand("SELECT A,B,C FROM TABLE",cnn);
SqlDataReader reader = cmd.ExecuteReader();
if (!reader.HasRow())
{
reader.Close();
return null;
}
while (reader.Read())
{
Object[] objs = new Object[reader.FieldCount];
reader.GetValues(objs);
//下面应该是你处理数据的代码
}
reader.close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |