使用 OleDbDataReader 使用未赋值的局部变量 c#



我遇到了问题,一直在撞墙......当我调用"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();
    }

你失踪了

OleDbCommand oCommand = new OleDbCommand (sql , dbConnection)

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

相反。。

您还应该使用 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
  }

你需要初始化 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();

相关内容

  • 没有找到相关文章

最新更新