2013-10-21 34 views
0

所以在C#中,我有一個ODBCDataReader返回它有行,但是當我嘗試訪問數據時,它返回的對象沒有設置爲對象錯誤的引用。我直接在數據庫上測試了sql,它確實返回了一行而沒有任何空值。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

您在使用塊之外使用它。在使用塊(在ExecuteReader()調用之後立即)內移動使用[results]的地方,你應該處在一個更好的地方。