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