2015-11-20 22 views
-1

我已將Visual Studio 2013連接到Oracle XE 11數據庫。我試圖使用下面的代碼從數據庫中的表中檢索信息,但似乎數組裝飾沒有使用我在foreach循環中設置的值進行初始化。 請幫忙。Null數組異常:未從Oracle數據庫初始化

conn.Open(); // it is an OleDbConnection 

String[] decor; 

DataTable table1 = new DataTable(); 

OleDbDataAdapter oda1 = new OleDbDataAdapter("select name from Product where Commodity_Type='decor'",conn); 

oda1.Fill(table1); 

int j=0; 

foreach (DataRow row in table1.Rows) 

      { 

       decor[j] = row["name"].ToString(); 

       j++; 

      } 

回答

0

試試這個代碼: 填寫表格後您需要initialized裝飾。

conn.Open(); 
DataTable table1 = new DataTable(); 
OleDbDataAdapter oda1 = new OleDbDataAdapter("select name from Product where Commodity_Type='decor'",conn); 
oda1.Fill(table1); 
String[] decor = new String[(table1.rows.Count())] 
int j=0; 
foreach (DataRow row in table1.Rows) { 
    decor[j] = row["name"].ToString(); 
    j++; 
} 
+0

非常感謝!它的工作:D –

3

你沒有初始化變量decor

decor = new String[(table1.rows.Count())] 

,這就是你應該自己已經發現了錯誤。

相關問題