2012-08-27 58 views
-2

我有下面的代碼,它曾經工作,直到我最近更新函數返回true或false。但突然間對象objReader停止在函數外可訪問。我已聲明在課程開始時作爲私人靜態oledbdatareader = null;以便我可以在當前課程中的任何方法中訪問它。oledbdatareader不能在C#中的函數之外訪問.net代碼

string strProvider = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strCurWBPath + ";;Mode=ReadWrite" + @";Extended Properties=""Excel 8.0;HDR=Yes;"""; 

      using (objConn = new OleDbConnection(strProvider)) 
      { 
       objConn.Open();      
       using (objCmd = new OleDbCommand(strQuery, objConn)) 
       { 
        objCmd.CommandType = CommandType.Text; 
        objCmd.ExecuteNonQuery(); 
        objReader = objCmd.ExecuteReader(CommandBehavior.SequentialAccess); 

        // No point reading/writing data if there are no rows. 
        if (objReader.HasRows) 
        { 

          if (!objReader.IsClosed) 
          { 
           return true; 
          } 
          else 
           return false;       


        } 
        else 
        { 
         MessageBox.Show("There are no Rows to process. "); 
        } 

       }//end of using1 

      }//end of using2 

有什麼建議嗎?

回答

1

糾正我,如果我錯了(和困惑是什麼OP說 - not accessible outside the function in C#.net code),但在這裏,因爲它的聯營connection配置不能使用OleDbDataReader object方法/功能之外(你已經using塊)。

如果您希望數據庫的結果應該以另一種方法使用,那麼我建議您使用OleDbDataAdapter.Fill方法填充DataTable