C# 如何从 DataReader 的当前行中取出 DataRow?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18511700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:27:10  来源:igfitidea点击:

How to get a DataRow out the current row of a DataReader?

c#datarowdatareader

提问by Rémi

Ok, I would like to extract a DataRowout a DataReader. I have been looking around for quite some time and it doesn't look like there is a simple way to do this.

好的,我想提取DataRow一个DataReader. 我已经环顾了很长一段时间,但似乎没有一种简单的方法可以做到这一点。

I understand a DataReaderis more a collection of rows but it only read one row at the time.

我知道 aDataReader更像是一组行,但它当时只读取一行。

So my question: is there any way to extract a DataRowout the current row of a DataReader?

所以我的问题:有没有办法提取 aDataRow的当前行DataReader

采纳答案by Tim Schmelter

Is there any way to extract a DataRow out the current row of a DataReader ?

有没有办法从 DataReader 的当前行中提取 DataRow ?

No, at least no simple way. Every DataRow belongs to one Table. You cannot leave that property empty, you cannot even change the table(without using ImportRow).

不,至少没有简单的方法。每个 DataRow 都属于一个Table。您不能将该属性留空,甚至不能更改表(不使用 ImportRow)。

But if you need DataRows, why don't you fill a DataTablein the first place?

但是,如果您需要 DataRows,为什么不首先填写 aDataTable呢?

DataTable table = new DataTable();
using(var con = new SqlConnection("...."))
using(var da = new SqlDataAdapter("SELECT ... WHERE...", con))
    da.Fill(table);
// now you have the row(s)

If you really need to get the row(s) from the DataReaderyou can use reader.GetSchemaTableto get all informations about the columns:

如果您确实需要从中获取行,则DataReader可以使用它reader.GetSchemaTable来获取有关列的所有信息:

if (reader.HasRows)
{
    DataTable schemaTable = reader.GetSchemaTable();
    DataTable data = new DataTable();
    foreach (DataRow row in schemaTable.Rows)
    {
        string colName = row.Field<string>("ColumnName");
        Type t = row.Field<Type>("DataType");
        data.Columns.Add(colName, t);
    }
    while (reader.Read())
    {
        var newRow = data.Rows.Add();
        foreach (DataColumn col in data.Columns)
        {
            newRow[col.ColumnName] = reader[col.ColumnName];
        }
    }
}

But that is not really efficient.

但这并不是真正有效的。

回答by Glenn Slayden

You're probably asking because you have DataReadercode that looks something like this:

您可能会问,因为您的DataReader代码如下所示:

static void ViaDataReader(IDataReader rdr)
{
    while (rdr.Read())
        Console.WriteLine("{0,-27} {1,-46} {2,-25} {3}", rdr[0], rdr[1], rdr[2], rdr[3]);
}

/// ...

ViaDataReader(DbProviderFactories.GetFactoryClasses().CreateDataReader());

But as Tim pointed out, if you know in advancethat you're going to want the DataRowat each iteration, you should use DataTableinstead, and then just iterate on its Rowsproperty (following output is identical to above):

但是,蒂姆指出的那样,如果你事先知道,你要去想DataRow在每次迭代,你应该使用DataTable来代替,然后只需迭代其Rows财产(以下输出等同于以上):

static void ViaDataTable(IDataReader rdr)
{
    var table = new DataTable();
    table.Load(rdr);
    foreach (DataRow row in table.Rows)
        Console.WriteLine("{0,-27} {1,-46} {2,-25} {3}", row[0], row[1], row[2], row[3]);
}

/// ...

ViaDataTable(DbProviderFactories.GetFactoryClasses().CreateDataReader());

If you absolutely must continue using the DataReaderfor some reason, I suppose you could add a loop index integer to that first example, and then grab each row from the (fully-pre-loaded DataTable) on each iteration. But since the DataTableis richer all-around, there's really no reason not to abandon the DataReaderafter doing the DataTable.Load().

如果DataReader出于某种原因您绝对必须继续使用,我想您可以在第一个示例中添加一个循环索引整数,然后DataTable在每次迭代时从(完全预加载)中获取每一行。但既然DataTable是更丰富的全能,真的没有理由不放弃DataReader做完之后的DataTable.Load()