日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DataTable操作

發(fā)布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataTable操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
DataTable操作

一 復(fù)制DataTable中符合條件的DataRow到新的DataTable中

?

  One:

DataTable TableTemp = new DataTable();//臨時table DataTable tableAd = new Web.DAL.FreeBase().TranSQLGetTable("select a.ClassName,b.ParentId,b.Name,b.Pic,b.Url,b.Sorts from AdClass a inner join Ad b on a.Id=b.ParentId");//查詢的結(jié)果if (tableAd != null && tableAd.Rows.Count > 0){Tableflag = tableAd.Copy();//復(fù)制結(jié)構(gòu)Tableflag.Clear();//清除臨時數(shù)據(jù)foreach (DataRow dr in tableAd.Rows){if (dr["ParentId"].ToString().Equals("17")){DataRow tempRow =TableTemp.NewRow(); //創(chuàng)建與該表相同架構(gòu)的新行
              tempRow["ClassName"] = dr["ClassName"];
              tempRow["ParentId"] = dr["ParentId"];
              tempRow["Name"] = dr["Name"];
              tempRow["Pic"] = dr["Pic"];
              tempRow["Url"] = dr["Url"];
              tempRow["Sorts"] = dr["Sorts"];
              TableTemp.Rows.Add(tempRow);
            }
          }
        }

Two:

#region DataTable篩選,排序返回符合條件行組成的新DataTable或直接用DefaultView按條件返回/// <summary> /// DataTable篩選,排序返回符合條件行組成的新DataTable或直接用DefaultView按條件返回 /// eg:SortExprDataTable(dt,"Sex='男'","Time Desc",1) /// </summary> /// <param name="dt">傳入的DataTable</param> /// <param name="strExpr">篩選條件</param> /// <param name="strSort">排序條件</param> /// <param name="mode">1,直接用DefaultView按條件返回,效率較高;2,DataTable篩選,排序返回符合條件行組成的新DataTable</param> public static DataTable SortDataTable(DataTable dt, string strExpr, string strSort, int mode){switch (mode){case 1://方法一 直接用DefaultView按條件返回 dt.DefaultView.RowFilter = strExpr;dt.DefaultView.Sort = strSort;return dt;case 2://方法二 DataTable篩選,排序返回符合條件行組成的新DataTable DataTable dt1 = new DataTable();DataRow[] GetRows = dt.Select(strExpr, strSort);//復(fù)制DataTable dt結(jié)構(gòu)不包含數(shù)據(jù) dt1 = dt.Clone();foreach (DataRow row in GetRows){dt1.Rows.Add(row.ItemArray);}return dt1;default:return dt;}}#endregion View Code //選取ParentId=17的所以行,并根據(jù)Sorts降序排序
TableTemp = SortDataTable(tableAd, "ParentId=17", "Sorts Desc", 2);

Three:

#region 獲取DataTable前幾條數(shù)據(jù)/// <summary> /// 獲取DataTable前幾條數(shù)據(jù) /// </summary> /// <param name="TopItem">前N條數(shù)據(jù)</param> /// <param name="oDT">源DataTable</param> /// <returns></returns> public static DataTable DtSelectTop(int TopItem, DataTable oDT){if (oDT.Rows.Count < TopItem) return oDT;DataTable NewTable = oDT.Clone();DataRow[] rows = oDT.Select("1=1");for (int i = 0; i < TopItem; i++){NewTable.ImportRow((DataRow)rows[i]);}return NewTable;}#endregion View Code

?

//選取前7行數(shù)據(jù)
TableTemp = DtSelectTop(7, Table1);

?

posted on 2015-07-03 14:43?無影飛絮劍 閱讀(...) 評論(...) 編輯 收藏

轉(zhuǎn)載于:https://www.cnblogs.com/itslives-com/p/4618627.html

總結(jié)

以上是生活随笔為你收集整理的DataTable操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。