2010-06-08 2 views
8

다음 코드가 있습니다.이 쿼리의 결과에서 모든 필드를 반복하고 필드라는 사전을 채 웁니다.어떻게 OracleDataReader의 모든 열을 루프 할 수 있습니까?

이 경우 데이터 제공자가 가능합니까?

  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(); 

답변

16

당신은 같은 것을 할 수 있어야한다 :

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); 
    }  
} 
4

GetSchemaTable 난 당신이 열 이름으로 사전의 키를 원하는 가정 이름뿐만 아니라 크기, 유형 등

포함하여 열에 대한 많은 정보를 반환하고합니다 값이 행 값이됩니다. 그렇다면,이 작업을해야합니다 : 당신은 또한 열 수를 얻을 수 GetValues()을 사용하고, 각 GetName(int)를 호출 할 수

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

.

관련 문제