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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

将DataTable 存到一个集合当中

發(fā)布時(shí)間:2023/12/18 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将DataTable 存到一个集合当中 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

將DataTable 存到一個(gè)集合中

此做法來自:http://www.codeproject.com/Articles/692832/Simple-way-of-using-SQL-DataTables-to-JSON-in-MVC

?

?

using?System;
using?System.Data;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;

namespace?MvcApplication31.ViewModels
{
????public?class?DataAccessLayer
????{
????????public?DataTable?GetTable()
????????{
????????????DataTable?dtTable?=?new?DataTable();
????????????dtTable.Columns.Add("UserID",?typeof(int));
????????????dtTable.Columns.Add("FirstName",?typeof(string));
????????????dtTable.Columns.Add("LastName",?typeof(string));

????????????dtTable.Rows.Add(25,?"Ave",?"Maria");
????????????dtTable.Rows.Add(50,?"Bill",?"Doe");
????????????dtTable.Rows.Add(75,?"John",?"Gates");
????????????dtTable.Rows.Add(99,?"Julia",?"Griffith");
????????????dtTable.Rows.Add(100,?"Mylie",?"Spears");
????????????return?dtTable;
????????}

????????public?List<Dictionary<string,?object>>?GetTableRows(DataTable?dtData)
????????{
????????????List<Dictionary<string,?object>>?lstRows?=?new?List<Dictionary<string,?object>>();
????????????Dictionary<string,?object>?dictRow?=?null;

????????????foreach?(DataRow?dr?in?dtData.Rows)
????????????{
????????????????dictRow?=?new?Dictionary<string,?object>();
????????????????foreach?(DataColumn?col?in?dtData.Columns)
????????????????{
????????????????????dictRow.Add(col.ColumnName,?dr[col]);
????????????????}
????????????????lstRows.Add(dictRow);
????????????}
????????????return?lstRows;
????????}
????}
}

?

?

?

以上代碼有兩個(gè)方法,一個(gè)是獲取一個(gè)DataTable的方法,這個(gè)我們可以自己用ADO.NET獲取,這里要說的是第二個(gè)方法,第二個(gè)方法的思想是:

將每一行存儲(chǔ)到一個(gè)鍵值對(duì)集合中,當(dāng)前行的每列用一個(gè)key-value對(duì)存儲(chǔ),最后將這些鍵值對(duì)集合存到List集合中,這樣就得到了一個(gè)List<Dictionary<string,object>> 類型的集合了。

?

當(dāng)我們將DataTable集合轉(zhuǎn)換成集合之后就可以很方便的將其傳到前臺(tái)頁(yè)面、或者View視圖了。

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/key1309/p/3460847.html

總結(jié)

以上是生活随笔為你收集整理的将DataTable 存到一个集合当中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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