1. 程式人生 > >將DataTable與DataView轉換成DataSet(示例)

將DataTable與DataView轉換成DataSet(示例)

今天遇到這樣的事情。將DataSet的檢視傳遞給DataView,對DataView進行了資料排序。然後想將 DataView再放到DataSet中。卻發現,DataSet的檢視狀態是不可以賦值的。即是隻讀狀態。

 當然頭一個反應就是到百度上去搜索。以“DataView轉換成DataSet”的關鍵字作為查詢。當然顯示的結果很令人滿意。查到了NNNN多。欣然開啟連結。哈哈,這個連結就到了csdn上了。樓主朋友遇到了和我一樣的問題,熱心的朋友在那熱心的留言幫助。總結各位熱心朋友的解釋。“DataView轉換成DataSet”那是不可能的。(我就不信,肯定有個解決的方法)。

    於是在百度上不斷的點連結,突然看到“DataView

這樣的物件儲存到 XML 實”!心裡那個激動啊。馬上點開連結。原來是msdn啊,終於找到官方的解釋了。

   說了這麼多廢話。馬上進入主題——————————————》》》》》

    ADO.NET 框架只對 DataSet 物件提供顯式 XML 支援。不過,將 DataView 或 DataTable 轉換為 XML 並不是特別難。在這兩種情況下,您都必須使用臨時的資料集作為要另存為 XML 的行集的容器。用於將 DataTable 儲存為 XML 所必需的程式碼很簡單。

以下是我做的一個示例。

1、用dataset讀取。xml檔案。

2、將dataset檢視傳給DataView,並對DataView進行排序。

3、將DataView的table傳遞給DataTable。

4、將DataTable新增給DataSet。然後用DataSet重新寫入XML檔案

user.xml檔案內容

<?xml version="1.0" encoding="gb2312"?>
<user>
  
<person name="name1">
    
<sex>M</sex>
    
<age>15</age>
    
<pass>1234</pass>
    
<Address>浙江杭州</Address>

  
</person>
  
<person name="fast">
    
<sex>F</sex>
    
<age>25</age>
    
<pass>5dfdfjsl</pass>
    
<Address>杭州拱墅區</Address>
  
</person>
  
<person name="tom">
    
<sex>M</sex>
    
<age>23</age>
    
<pass>放電放電</pass>
    
<Address>紹興新昌</Address>
  
</person>
  
<person name="charli">
    
<sex>F</sex>
    
<age>34</age>
    
<pass>大幅度放電</pass>
    
<Address>台州</Address>
  
</person>
  
<person name="jimm">
    
<sex>F</sex>
    
<age>54</age>
    
<pass>東方路djwoomldm</pass>
    
<Address>舟山東路</Address>
  
</person>
  
<person name="frinm">
    
<sex>M</sex>
    
<age>35</age>
    
<pass>23fds</pass>
    
<Address>beijing china </Address>
  
</person>
  
<person name="agui">
    
<sex>F</sex>
    
<age>36</age>
    
<pass>輔導室閥</pass>
    
<Address>魅力的中國</Address>
  
</person>
<person name="one">
    
<sex>F</sex>
    
<age>3</age>
    
<pass>輔導室閥</pass>
    
<Address>魅力的中國</Address>
  
</person>
<person name="three">
    
<sex>F</sex>
    
<age>300</age>
    
<pass>輔導室閥</pass>
    
<Address>魅力的中國</Address>
  
</person>
</user>

CS程式碼

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

publicpartialclass Sort : System.Web.UI.Page
{
    
protectedvoid Page_Load(object sender, EventArgs e)
    
{
        DataSet ds 
=new DataSet();
        ds.ReadXml(Server.MapPath(
"user.xml"));
        DataColumn dc 
=new DataColumn("Sort");
        dc.DataType 
=typeof(int);
        ds.Tables[
0].Columns.Add(dc);

        
for (int i =0; i < ds.Tables[0].Rows.Count;i++ )
        
{
            ds.Tables[
0].Rows[i]["Sort"= ds.Tables[0].Rows[i]["age"];
        }

        
int ordinal = ds.Tables[0].Columns["age"].Ordinal;
        ds.Tables[
0].Columns.Remove("age");
        ds.Tables[
0].Columns["Sort"].ColumnName ="age";
        ds.Tables[
0].Columns["age"].SetOrdinal(ordinal);
        DataView dv
=new DataView();
        dv 
= ds.Tables[0].DefaultView;
        dv.Sort 
="age asc";
        
this.Gridview1.DataSource = dv;
        
this.Gridview1.DataBind();
        WriteDataTableToXml(Server.MapPath(
"."+"/shengcheng.xml", DataViewToDataTable(dv));

        ds 
=new DataSet();
        ds.ReadXml(Server.MapPath(
"shengcheng.xml"));
        dv 
= ds.Tables[0].DefaultView;
        
this.Gridview2.DataSource = dv;
        
this.Gridview2.DataBind();

    }


   
public DataTable DataViewToDataTable(DataView dv)
    
{
        
//Clone dv.Table to dtTemp
        DataTable dtTemp = dv.Table.Clone();
        dtTemp.TableName 
="Row";   

        
foreach (DataRowView drv in dv)
            dtTemp.ImportRow(drv.Row);
        
return dtTemp;
    }


   
publicvoid WriteDataTableToXml(String fileName, DataTable dt)
    
{
        DataSet dsTmp 
=new DataSet();
        DataTable dtTmp 
= dt.Copy();
        dsTmp.Tables.Add(dtTmp);

        StreamWriter sr 
=new StreamWriter(fileName);
        dsTmp.WriteXml(sr);
        sr.Close();
    }

//    protected int Sort {
//    int get ();
//    void set (String value);
//}




}

Sort.aspx

%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sort.aspx.cs" Inherits="Sort"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>無標題頁</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    從舊檔案讀取排序後顯示的檔案格式
    
<asp:gridview ID="Gridview1" runat="server"></asp:gridview>
    
    
<br />
   
<font color=red> 從新檔案讀取沒有排序後顯示的檔案格式</font>
    
<asp:gridview ID="Gridview2" runat="server"></asp:gridview>
    
</div>
    
</form>
</body>
</html>

在此特別說一句,datatable在asp.net2.0中已經支援對xml檔案的讀寫操作了

對於以上還用dataset來操作主要是參照了asp.net1.1中的做法

相關推薦

DataTableDataView轉換DataSet示例

今天遇到這樣的事情。將DataSet的檢視傳遞給DataView,對DataView進行了資料排序。然後想將 DataView再放到DataSet中。卻發現,DataSet的檢視狀態是不可以賦值的。即是隻讀狀態。  當然頭一個反應就是到百度上去搜索。以“DataView轉換成

C# DataTable對象轉換XML字符串

summary see lB tex int finally n) exce row /// <summary> /// 將DataTable對象轉換成XML字符串 /// </summary>

PHP curl後json_decode無法json轉換陣列

整了嗯久,PHP curl後json_decode無法將json轉換成陣列;最後才得到原因: curl返回的資料中帶有bom格式,需要轉換; 有些返回資料直接: print_r(json_decode($data,true));  就可以轉換。   

字串轉換整數Java

題目:字串轉換為整數。 思路:將字串轉化為整數首先是遍歷字串中的每一個字元,有三種情況:首字元是正號,首字元是負號,首字元非正負號;然後遍歷每一個字元進行num = num * 10 + charAr

字串轉換整數atoi函式的具體實現

程式碼如下: #include "stdio.h" int Atoi(char* str) {int sum=0;while(*str!='\0'){if (*str>='0' && *str<='9'){sum=sum*10+*str-'0';

ios XML,JSON,陣列解析並轉換NSMutableArrayList

一、簡介 JSON解析: 通過正則將JSON([{...},{...}...])分解成多個包含實體例項內容的一小節({...}),在一小節中通過Runtime(執行時)(<objc/runtime.h>)將實體所有屬性值找到並賦值(正則查詢實現)到例項({...

把字串轉換整數字串

題目描述:將一個字串轉換成一個整數,要求不能使用字串轉換整數的庫函式。 數值為0或者字串不是一個合法的數值則返回0。 輸入描述:輸入一個字串,包括數字字母符號,可以為空 輸出描述:如果是合法的數值表達則返回該數字,否則返回0  思路一: public class Solu

c#List&lt;T&gt;轉換DataSet

foreach for new bsp ack popu per summary [] /// <summary> /// List<T> 轉換成DataSet /// </summary&g

帶下劃線的字串轉換大寫下劃線後大寫的高效方法

如test_tb_kkk_llll  轉換為    TestTbKkkLlll 原理: 1. 判斷是否包含下劃線     (1) 包含:     &

typescript簡單物件陣列轉換父子結構具有children屬性的物件

轉換效果 initDat=[ { title: '節點1', pkey: '-1', key: '2', }, { title: '節點2', pkey: '2', key: 'test1', }, { titl

如何一維陣列轉換考慮南天陣列元素?

我有一個像下面的列表,我想將這個元素分解成n維基於NaN值的禮物。 輸入: [nan 0.1 0.4 0.6 nan 0.8 0.7 0.9 nan 0.3 0.6 0.8] 輸出: [[0.1 0.4 0.6] [0.8 0.7 0.9] [0.3 0.6 0.8]] 如何實現

劍指offer之字串轉換整數Java實現

將字串轉換成整數 NowCoder 題目描述: 將一個字串轉換成一個整數(實現Integer.valueOf(string)的功能,但是string不符合數字要求時返回0),要求不能使用字串轉換整數的庫函式。 數值為0或者字串不是一個合法的數值則返回0。 輸入描述: 輸入一個

Oracle SQL欄位所有的值轉換數字忽略不匹配的值

最近由於業務需要,將某個欄位的值(Varchar2型別)轉換成數字。  由於原始資料比較亂,在將該欄位直接轉換成數字時,由於存在非數字字元(英文字母、漢語),直接轉換時,轉換失敗。 因此需要將這些欄位值轉換成0,將其他正常數字進行正常轉換,簡單範例如下: SELECT A1

java漢字轉換漢語拼音pinyin4j.jar的使用及原理

一、思路介紹 見漢字的unicode碼和漢語拼音對應,這也是pinyin4j的思路 二、pinyin4j介紹 pinyin4j使用了一個.txt的文字檔案(uicode_to_hanyu_pinyin.txt)用來儲存漢字unicode編碼與拼音的對應關係,通過讀取該配置

用Latex排版論文1如何Visio畫圖檔案轉換Latex支援的.eps檔案

選中刪除白邊距,然後點選確定就剪裁好了,如下圖所示: 按照下面的步驟執行:檔案->另存為->更多選項->內嵌式PostScript  這樣全部工作就完成了。因為Adobe Acrobat Pro的版本不同,可能步驟上有所差異,不過大體都差不多。希望對大家有幫助。 這裡還是要說一下,在lat

JSunicode碼轉中文方法解決IE8對JSON.stringify中文轉換unicode的問題

第①種情況:(無特殊字元) // 把json物件轉為json串 var stringcontent = JSON.stringify(data.jsonObject); //此時變數為:stringcontent={policy":[{"name":"must_inst

泛型資料轉換datatable

泛型類: public class MoudeType { public string RGuid { get; set; } pu

json轉換list不用導包

JSONArray array = new JSONArray(String); JSONObject jsonOb = null; User user = null; for (int i = 0; i < array.length(); i++) {     use

android時間戳轉換日期php後臺,日期轉換時間戳小問題記錄

之前常用的是,和java開發的後臺對接資料,正常也沒有遇到過這個問題,又一次在和php對接的後臺做專案是,時間轉換出了問題,我看了些許時間的程式碼,都沒有問題。最後發現返回時間戳的位數不對,才確認到,問題是出現在這裡 PHP和Java時間戳存在三位位差!!!