2013-10-21 3 views
0

C#에서는 행이 있음을 반환하는 ODBCDataReader가 있지만 데이터에 액세스하려고하면 개체 오류 참조로 설정되지 않은 개체가 반환됩니다. db 직접 SQL을 테스트하고 null이없는 행을 반환합니다.ODBCDataReader에는 행이 있지만 데이터에 액세스 할 수 없습니다.

OdbcDataReader results; 
try 
{ 
// Initialize & open odbc connection 
using (OdbcConnection conn = new OdbcConnection(connectionString.ToString())) 
{ 
    conn.Open(); 

    // Initialiaze odbc command object 
    using (OdbcCommand comm = new OdbcCommand(query.ToString(), conn)) 
    { 
     results = comm.ExecuteReader(); 

    } 
} 
} 
catch 
{ 
//detailed error messaging here (which does not get hit) 
} 

temp = results; 

if (temp.HasRows == false) 
{ 
//error messaging here does not get hit. 
} 
while (temp.Read()) 
{ 
    try 
    { 
     //I attempted to access the data by creating an object array: 
     object [] objarray = new object[temp.FieldCount) 
     temp.GetValues(objarray); //this causes error 
    } 
    catch{ // error is caught here "object not set to a reference of an object" } 

    for (i = 0; i < temp.FieldCount; i++) 
{ 
    try 
    { 
        //I also attempted other ways to access the data including: 
     temp[i].ToString(); // this causes error 
     temp.GetInt32(i).ToString(); // this causes error 
        temp.GetName(i); //this causes error 
    } 
    catch 
    { 
     // error is caught here "object not set to a reference of an object" 
    } 
} 
} 
+0

답변을 게시하려고했지만 표시되지 않아서 여기에서 시도해 보겠습니다. 문제는 코드와 관련이 없으며 (올바르게 작동 함) 데이터베이스 동기화 문제였습니다. – user2904607

답변

2

사용중인 블록 외부에서 사용하고 있습니다. using 블록 (ExecuteReader() 호출 직후)에서 [results]를 사용하는 부분을 이동하면 훨씬 더 좋은 위치에 있어야합니다.

관련 문제