您好,登录后才能下订单哦!
如何在C#中使用DataTable遍历数据?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
public static void Test2() { Stopwatch watch = new Stopwatch(); using (DbConnection conn = SqlHelper.GetConnection("ConnectionString")) { using (SqlCommand cmd = new SqlCommand()) { watch.Start(); cmd.CommandText = string.Format(@" select ROW_NUMBER() OVER (Order by S.AccID,S.CurrID,S.AbsID,S.Flag)AS RowNum,S.* from 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 Stopwatch(); watch3.Start(); DataTable newTable = HandleAccYear(table,true); 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(table); return table; }
private static DataTable HandleAccYear(DataTable dt, bool isCurrency) { DataTable newdt = dt.Clone(); //不使用主键 //dt.PrimaryKey = new DataColumn[] { // dt.Columns["AccID"], // dt.Columns["Flag"], // dt.Columns["AbsID"], // dt.Columns["RowNum"], //}; if (dt.Rows.Count > 0) { object flag = null; foreach (DataRow row in dt.Rows) { flag = row["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 = row["AccID"].ToString(), year = row["Y"].ToString(), absID = row["AbsID"].ToString(); newRow["Flag"] = "5"; newRow["SumInfo"] = "测试数据"; string filter = "AccID='" + accID + "' AND Y='" + year + "' AND AbsID <= " + absID; if (!isCurrency) { filter = "AccID='" + accID + "'AND CurrID='" + row["CurrID"] + "' AND Y='" + year + "' AND AbsID <= " + absID; } DataRow[] selectRow = dt.Select(filter); double debitLC = 0, debitQty = 0, creditLC = 0, creditQty = 0, debitFC = 0, creditFC = 0; foreach (DataRow item in selectRow) { debitLC += ToDouble(item["YearDebitLC"]); debitQty += ToDouble(item["YearDebitQty"]); creditLC +=ToDouble(item["YearCreditLC"]); creditQty += ToDouble(item["YearCreditQty"]); if (!isCurrency) { debitFC += ToDouble(item["YearDebitFC"]); creditFC += ToDouble(item["YearCreditFC"]); } } newRow["CurDebitLC"] = debitLC; newRow["CurDebitQty"] = debitQty; newRow["CurCreditLC"] = creditLC; newRow["CurCreditQty"] = creditQty; //newRow["CurDebitLC"] = dt.Compute("Sum(YearDebitLC)", filter); //newRow["CurDebitQty"] = dt.Compute("Sum(YearDebitQty)", filter); //newRow["CurCreditLC"] = dt.Compute("Sum(YearCreditLC)", filter); //newRow["CurCreditQty"] = dt.Compute("Sum(YearCreditQty)", filter); if (!isCurrency) { //newRow["CurCreditFC"] = dt.Compute("Sum(YearCreditFC)", filter); //newRow["CurDebitFC"] = dt.Compute("Sum(YearDebitFC)", filter); newRow["CurCreditFC"] = creditFC; newRow["CurDebitFC"] = debitFC; } newdt.Rows.Add(sourceRow); newdt.Rows.Add(newRow); } else { DataRow sourceRow = newdt.NewRow(); sourceRow.ItemArray = row.ItemArray; newdt.Rows.Add(sourceRow); } } } return newdt; }
当不使用主键进行遍历计算插入相应的值时所用时间竟然是这么多:
当我使用同样的方法,同样的数据添加主键(即把HandleAccYear方法中不使用主键下面的注释去掉后).进行遍历计算等操作,得出的结果竟然有这么大的差别:
补充:C# DataTable数据量大,循环处理数据的时候优化速度
相信大家用for循环datatable数据的不会太少,这个在数据量比较小的时候可以接受,但是数据量大的时候却会造成CPU占用过高,甚至把电脑资源耗尽卡死至无限等待,
其实一些循环耗时的操作可以用线程池分块来处理,这样会减轻CPU很多压力,好比食堂打饭,当只有一个窗口的时候势必等待的时间会非常的长,但是多开几个窗口的时候却大大提高效率,
C#中用线程池就可以做到,本来一开始的时候我用的是为每个区块开一个线程,但是有一个问题就是开了那么多的线程没办法结束他们,后来我想到了线程池,
具体代码如下:
int sid = dt.Rows.Count % 100 == 0 ? (dt.Rows.Count / 100) : (dt.Rows.Count / 100 + 1); for (int a = 1; a <= sid; a++) { object aa=a.ToString() + "," + sid.ToString(); ThreadPool.QueueUserWorkItem(todo , aa); } public void todo(object aa) { string sql = ""; int startindex = Convert.ToInt32(aa.ToString().Split(',')[0]); int limitstep = Convert.ToInt32(aa.ToString().Split(',')[1]); for (int i = (startindex > 1 ? ((startindex - 1) * 100) : 0); i < (startindex == limitstep ? (dt.Rows.Count) : startindex*100); i++) { //todo数据操作 } Thread.Sleep(2000); }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。