我当前的代码循环遍历 DataTable 对象的特定列的所有行。我希望它只循环到倒数第二个位置。我该怎么做 ?
我知道这可以通过 for 循环而不是我的 foreach 来完成。但是,我不知道如何获取行数,然后根据索引逐行迭代。那就是我需要帮助的地方。
public void Main()
{
OleDbDataAdapter oleDA = new OleDbDataAdapter();
DataTable dt = new DataTable();
DataColumn col = null;
DataRow row = null;
string strCols = null;
oleDA.Fill(dt, Dts.Variables["ExecuteSQLTask_ResultSet"].Value);
col = dt.Columns["AColumInDataTable"];
foreach (DataRow row_ in dt.Rows)
{
row = row_;
strCols = strCols + row[col.Ordinal].ToString() + ", ";
}
strCols = strCols.Substring(0, strCols.Length - 2);
MessageBox.Show("Rows of a column contain - " + strCols);
Dts.TaskResult = (int)ScriptResults.Success;
}