我遇到問題並且一直在敲我的頭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();
}
當然你得到這個錯誤。你永遠不會初始化'dbCommand'。 –
如果有疑問,請相信編譯器錯誤真的*是*正確的... –
Jon Skeet ... Upvote! –