介绍
如何在c#中使用数据表遍历数据?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
public static void Test2 () { ,Stopwatch watch =, new 秒表(); ,using (DbConnection conn =, SqlHelper.GetConnection (“ConnectionString")) ,{ using 才能;(SqlCommand cmd =, new SqlCommand ()) {才能 ,,watch.Start (); ,,cmd.CommandText =, string.Format (@" select ROW_NUMBER(),配套;(Order by  S.AccID, S.CurrID, S.AbsID, S.Flag) AS RowNum, s *,得到Test S “); ,,cmd.Connection =, conn as SqlConnection; ,,cmd.CommandTimeout =, 60000; ,,conn.Open (); ,,DataTable table =, ExecuteDataTable (cmd); ,,watch.Stop (); ,,Console.WriteLine(“从数据库取出数据{0}条“,,table.Rows.Count); ,,Stopwatch watch3 =, new 秒表(); ,,watch3.Start (); ,,DataTable newTable =, HandleAccYear(表,真实); ,,watch3.Stop (); ,,Console.WriteLine(“数据{0},遍历操作时间:毫秒:{1},秒:{2},,,newTable.Rows.Count,, watch3.ElapsedMilliseconds,, watch3.ElapsedMilliseconds /, 1000); ,,} conn.Close才能(); ,} }
填充数据到DataTable的方法
public static DataTable ExecuteDataTable (SqlCommand cmd) { DataTable 才能;table =, new DataTable (); SqlDataAdapter 才能;adaper =, new SqlDataAdapter (cmd); adaper.Fill才能(表); return 才能;表; }
private static DataTable HandleAccYear (DataTable , dt, bool isCurrency) { DataTable 才能;newdt =, dt.Clone ();//才能不使用主键//dt.PrimaryKey 才能=,new 上[],{//才能,dt.Columns (“AccID"),//才能,dt.Columns (“Flag"),//才能,dt.Columns (“AbsID"),//才能,dt.Columns (“RowNum"),//}才能; if 才能;(dt.Rows.Count 祝辞,0) {才能 ,,object flag =,空; ,,foreach (DataRow row  dt.Rows拷贝) ,,{ ,,,flag =,行(“Flag"); ,,,if (flag !=, null ,,, ! Helper.AreEqual (flag.ToString (),“1“)) ,,,{ ,,,,DataRow newRow =, newdt.NewRow (); ,,,,DataRow sourceRow =, newdt.NewRow (); ,,,,sourceRow.ItemArray =, row.ItemArray; ,,,,newRow.ItemArray =, row.ItemArray; ,,,,string accID =,行(“AccID") .ToString (), ,,,,,year =,行(“Y") .ToString (), ,,,,,absID =,行(“AbsID") .ToString (); ,,,,启动(“Flag"),=,“5”; ,,,,启动(“SumInfo"),=,“测试数据“; ,,,,string filter =,“AccID=& # 39;“, +, accID +,“& # 39;,以及Y=& # 39;“, +, year +,“& # 39;,以及AbsID & lt;=,,, +, absID; ,,,,if (isCurrency !) ,,,,{ ,,,,,filter =,“AccID=& # 39;“, +, accID +,“& # 39;以及克里德=& # 39;“,+,行(“CurrID"), +,“& # 39;,以及Y=& # 39;“, +, year +,“& # 39;,以及AbsID & lt;=,,, +, absID; ,,,,} ,,,,我们[],selectRow =, dt.Select(过滤器); ,,,,double debitLC =, 0,, debitQty =, 0,, creditLC =, 0,, creditQty =, 0,, debitFC =, 0,, creditFC =, 0; ,,,,foreach (DataRow item  selectRow拷贝) ,,,,{ ,,,,,debitLC +=, ToDouble(项目(“YearDebitLC")); ,,,,,debitQty +=, ToDouble(项目(“YearDebitQty")); ,,,,,creditLC +=ToDouble(项目(“YearCreditLC")); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在c#中使用数据表遍历数据