2015-09-26 19 views
0

的所有recors在以下代碼中,我希望只讀取一次數據集記錄以避免數據庫事務,並且希望按照第一行迭代循環。按照第一行迭代Foreach循環以獲得表

//InitializeConfig method return dataset which contains 6 records 
// i.e. Table with 6 rows and 6 columns 

dsConfig = objConfig.InitializeConfig(objConfig, NoOfGates); 
foreach (DataRow dr in dsConfig.Tables[0].Rows) 
{ 
    objCR.GetCard(objConfig.iIndex, objConfig.iCRNo, out MainId); 
} 

但我總是從第一行開始只記錄每次迭代的記錄。

+0

這裏我只想從數據集中讀取記錄一次,並希望徹底使用它。 –

+0

你的問題和你的代碼對我來說不清楚,但我注意到你沒有在'foreach'循環中的任何地方使用'dr'。 – venerik

+0

試試這個:foreach(DataRow dr in dsConfig.Tables [0] .AsEnumerable()) – jdweng

回答

0

這個問題還不是很清楚,但請讓我對venerik發表評論。當你在循環體內不使用它們時迭代行是毫無意義的。

foreach (DataRow dr in dsConfig.Tables[0].Rows) 
{ 
    // starting from here the variable dr is available 
    Console.WriteLine(dr["GateNo"].ToString()) // this should print the value of GateNo column for each row 
    objCR.GetCard(objConfig.iIndex, objConfig.iCRNo, out MainId); 
} 
0

這裏沒有必要使用Foreach循環簡單,因爲從數據集我iIndex和iCRNo參數傳遞給GetCard方法我可以循環for循環我的DataSet對象

for(i=0; i<dsConfig.Rows.Count; i++) 
{ 
objCR.GetCard(Convert.ToInt32(dsConfig.Tables[0].Rows[i].ItemArray[2]), Convert.ToInt32(dsConfig.Tables[0].Rows[i].ItemArray[3]), out MainId); 
}