使用OleDbDataReader使用未分配的局部变量c#

时间:2012-06-22 19:01:31

标签: c# oledbdatareader

我遇到了问题并且一直在敲打我的脑袋...当我调用“dbReader = dbCommand.ExecuteReader();”时,我不断获得“使用未分配的局部变量”它说使用未分配的局部变量'dbCommand'。有人请看看这个并告诉我在哪里以及我做错了什么?先谢谢你。

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

2 个答案:

答案 0 :(得分:2)

你错过了

OleDbCommand oCommand = new OleDbCommand (sql , dbConnection)

您正在初始化dbAdapter = new OleDbDataAdapter(sql, dbConnection);

代替..

你也应该使用Dispose的使用语句并关闭连接和读者而不是

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
  }

答案 1 :(得分: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();