how can i loop through all of the columns of the OracleDataReader

前端 未结 2 399
不思量自难忘°
不思量自难忘° 2020-12-31 15:22

I have the following code and i want to loop through all the fields in the result of this query and populate the dictionary called field.

Given a datareader is this

2条回答
  •  灰色年华
    2020-12-31 15:53

    GetSchemaTable will return a lot of information about the columns, including their name but also size, type, etc.

    I presume you want the key of the dictionary to be the column name, and the value to be the row value. If so, this should work:

    var dict = reader.GetSchemaTable().Rows.OfType().Select(
        r => r["ColumnName"].ToString()
    ).ToDictionary(
        cn => cn,
        cn => reader[cn].ToString()
    );
    

    You could also use GetValues() to get the number of columns, and call GetName(int) for each.

提交回复
热议问题