2012-06-22 44 views
-3

我遇到问题并且一直在敲我的头aginst墙...当我调用“dbReader = dbCommand.ExecuteReader()”时,我总是收到“使用未分配的本地变量” ;”它说使用未分配的本地变量'dbCommand'。请有人看看这个,并告诉我我在哪里做什么错了?先进的谢谢你。使用未分配的本地变量c#使用OleDbDataReader

public void computerList() 
    { 
     //Create SQL strings 
     string sql = "SELECT Computers FROM [Sheet1$]"; 

     //Create the instances 
     OleDbConnection dbConnection; 
     OleDbDataAdapter dbAdapter; 
     OleDbCommand dbCommand; 
     OleDbDataReader dbReader; 
     DataTable dataTable; 

     //Call the instance 
     dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.4.0;Data Source=Y:\\\\serverName\\Share\\document.xls;Extended Properties='Excel 12.0 Xml;HDR=YES'"); 
     dbAdapter = new OleDbDataAdapter(sql, dbConnection); 
     dataTable = new DataTable(); 
     dbConnection.Open(); 
     dbReader = dbCommand.ExecuteReader(); 

     while (dbReader.Read()) 
     { 
      int iRow = dataTable.Rows.Count; 
      //MessageBox.Show("Count " + iRow.ToString()); 
      //MessageBox.Show(dbReader.ToString()); 
      for (int i = 0; i < iRow; i++) 
      { 
       int loopID = i; 
       string rowData = dataTable.TableName; 
       MessageBox.Show("Count" + loopID); 
       MessageBox.Show(dbReader.GetString(iRow)); 
      } 

     } 
     //Close Connections 
     dbReader.Close(); 
     dbConnection.Close(); 
    } 
+5

当然你得到这个错误。你永远不会初始化'dbCommand'。 –

+4

如果有疑问,请相信编译器错误真的*是*正确的... –

+0

Jon Skeet ... Upvote! –

回答

2

你缺少

OleDbCommand oCommand = new OleDbCommand (sql , dbConnection) 

要初始化dbAdapter = new OleDbDataAdapter(sql, dbConnection);

,而不是..

也是英语新HOULD使用using语句对于处置和关闭的连接和读者,而不是

dbReader.Close(); 
dbConnection.Close(); 

我把它改写这样的...

public void computerList() 
    { 
     //Create SQL strings 
     string sql = "SELECT Computers FROM [Sheet1$]";  

     using (OleDbConnection dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.4.0;Data Source=Y:\\\\serverName\\Share\\document.xls;Extended Properties='Excel 12.0 Xml;HDR=YES'")) 
     { 
      //dbAdapter = new OleDbDataAdapter(sql, dbConnection); //You dont need it 
      //dataTable = new DataTable(); //don't need it 
      dbConnection.Open(); 
      using(OleDbCommand oCommand = new OleDbCommand (sql , dbConnection)) 
      { 
       using(OleDbDataReader dbReader = dbCommand.ExecuteReader()) 
       { 
        while (dbReader.Read()) 
        { 
         //int iRow = dataTable.Rows.Count; //always zero you never used the datable 
         //MessageBox.Show("Count " + iRow.ToString()); 
         //MessageBox.Show(dbReader.ToString()); 
         for (int i = 0; i < dbReader.FieldCount; i++) 
         { 
          //int loopID = i; //dont need it 
          //string rowData = dataTable.TableName; //Dont need it 
          MessageBox.Show("Count" + i); 
          MessageBox.Show(dbReader.GetString(i)); 
         } 

        } 
       } //reader closed and disposed   
      }//command disposed 
     } //connection closed and disposed 
    } 
+0

谢谢!我不敢相信我看过那一行!就是这样!你摇滚! – Fidelis

+0

如果你对这个答案感到满意,并且你认为这是所有人的最佳答案,不要忘记将其标记为已接受的答案。很高兴知道它有帮助 –

0

您需要初始化的DbCommand

//Call the instance 
dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.4.0;Data Source=Y:\\\\serverName\\Share\\document.xls;Extended Properties='Excel 12.0 Xml;HDR=YES'"); 
dbCommand = new OleDbCommand("SELECT * FROM TableName", dbConnection); 
dbAdapter = new OleDbDataAdapter(sql, dbConnection); 
dataTable = new DataTable(); 
dbConnection.Open(); 
dbReader = dbCommand.ExecuteReader(); 
+0

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader(v=vs.71).aspx - 说你不这样做。它说你调用OleDbCommand.ExecuteReader。 – Fidelis

+0

@ Spontaneous1980该文档引用dbReader,它由'dbCommand.ExecuteReader()'函数创建。顺便说一下,你的dataTable是空的。 – LarsTech