小编典典

我如何遍历OracleDataReader的所有列

sql

我有以下代码,我想遍历此查询结果中的所有字段并填充名为field的字典。

给定一个数据读取器,这可能吗?

            OracleCommand command = connection.CreateCommand();
            string sql = "Select * from MYTABLE where ID = " + id;
            command.CommandText = sql;

            Dictionary<string, string> fields = new Dictionary<string, string>();
            OracleDataReader reader = command.ExecuteReader();

阅读 320

收藏
2021-03-17

共1个答案

小编典典

您应该能够执行以下操作:

Dictionary<string, string> fields = new Dictionary<string, string>();
OracleDataReader reader = command.ExecuteReader();

if( reader.HasRows )
{
    for( int index = 0; index < reader.FieldCount; index ++ )
    {
        fields[ reader.GetName( index ) ] = reader.GetString( index );
    }    
}
2021-03-17