Ajax.NET Pro(AjaxPro)——DataSet Examples(數據集例子)

DataSet Examples

The first example will read an XML file form the hard disk and return it as an DataSet object. The XML looks like this:

第一個例子:

從硬盤上讀取一個XML文件,並將其以DataSet對象的格式返回。

XML文件的格式如下:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="de-DE">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="uid" type="xs:short" minOccurs="0" />
                <xs:element name="name" type="xs:string" minOccurs="0" />
                <xs:element name="type" type="xs:string" minOccurs="0" />
                <xs:element name="refdate" type="xs:dateTime" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <uid>1</uid>
    <name>sysobjects</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>sysindexes</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>syscolumns</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>systypes</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>syscomments</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
</NewDataSet>

PS:一個DataSet中包含多個DataTable,DataSet標記裏,上邊定義了模式,下邊爲數據。

每個表格包含的內容爲模板定義了的屬性,幷包含一個實例的屬性值。

On the server-side .NET code I use this C# code to read the file and return it as an DataSet object:

在服務器端的.NET代碼中,使用如下的C#代碼來讀取XML文件,並將其返回爲一個DataSet對象。

[AjaxMethod]
public DataSet GetDataSet()
{
  DataSet ds = new DataSet();

  ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/data/dataset.xml"),
               XmlReadMode.ReadSchema);

  return ds;
}
On the client-side JavaScript code you get a new Ajax.Web.DataSet object. Using this object you can get the columns and rows for each table inside the dataset. Also you have several methods like .addColumn or .addRow.

在客戶端的JS代碼中,獲取到一個Ajax.Web.DataSet對象。使用該對象,可以獲取DataSet對象中的DataTable以及其中的行Row和列Col,同樣,也有諸如addRow和addColumn這樣的方法來操作獲取的DataSet對象。

function doTest1() {
  AJAXDemo.Examples.DataSets.Demo.GetDataSet(_callback1);
}

function _callback1(res) {
  var cols = res.value.Tables[0].Columns.length;
  var rows = res.value.Tables[0].Rows.length;
	
  alert(cols + " cols and " + rows + " rows");
}
PS:返回的DataSet對象,需要通過氣value屬性來獲取Tables,而非直接獲取Tables,通過索引可以指定要獲取的Table,然後可以獲取其行和列的集合。

Click here to get the count of columns and rows for the XML file. You can use the DataAdapter to fill a DataSet from a SQL Server or any other .NET database.

You can use any DataSet object you got from an Ajax.NET method to use it as an argument again. Following JavaScript code will get the DataSet from the first example and use it as an argument:

點擊便可以獲取XML文件的行和列。不僅僅可以通過XML文件來獲取一個DataSet對象,也可以使用DataAdapter來填充一個DataSet對象。(通過SqlServer或者其他任意的.NET支持數據庫)。

可以將從Ajax.NET方法中獲取的任意數據庫對象用作參數傳遞給其他方法。以下的JS代碼獲取第一個例子中得到的DataSet對象,並且將其用作參數。

function doTest3() {
  var ds = AJAXDemo.Examples.DataSets.Demo.GetDataSet().value;	// sync invoke of GetDataSet
  AJAXDemo.Examples.DataSets.Demo.GetXmlFromDataSet(ds, _callback2);
}
Click here to get the XML source of the DataSet as a string.

點擊回去XML文件的string格式。

Creating a DataSet on the client-side JavaScript

在客戶端通過JS來創建一個DataSet對象

With the new version you can create a DataSet directly on the client-side JavaScript code. You may use this objects to save cart items or if you have other items that can be saved in tables. See following JavaScript code that will create a DataSet:

在AjaxPro的新版本中,可以直接在客戶端通過JS代碼來創建一個DataSet對象。你可能需要一個DataSet對象來保存卡片信息或者其他的信息,以下大JS代碼掩飾瞭如何在客戶端創建一個DataSet來保存你從客戶端獲取的信息。

var ds = new Ajax.Web.DataSet();
var dt = new Ajax.Web.DataTable();
	
dt.addColumn("FirstName", "System.String");
dt.addColumn("Age", "System.Int32");
	
dt.addRow({"FirstName":"Michael","Age":28});
dt.addRow({"FirstName":"Tanja","Age":25});
	
ds.addTable(dt);
PS:添加列時,指定列名稱和列的數據類型。

添加行時,使用JS對象字面量(JSON格式)來添加一條記錄。

In this short example I configure two columns, the first one will hold a string, and the second column Age will use a integer. Now, we can add rows to the DataTable dt. There are two ways to add a new row:

在這個簡短的例子中,我配置了兩列,第一列保存一個string類型的數據,第二列,Age列,保存integer類型數據。然後就可以往DataTable中添加數據行了。添加行的時候,有兩種方法:

dt.addRow({"FirstName":"Michael","Age":28});

// or using an object

var row = new Object();

row.FirstName = "Michael";
row.Age = 28;

dt.addRow(row);
第一種方法是,直接在AddRow的參數中初始化一個符合該Table行定義的記錄。

第二種方法,先創建一個行對象,然後爲其屬性賦值,最後將行添加到表中。

PS:第一種方法簡單明瞭,除非有必要,還是使用第一種好。

Click here to create such a DataSet using JavaScript, send it to a Ajax.NET method and get the XML string back of the DataSet.

You can now save any data on the server that you may collect on the client.

點擊來創建一個DataSet對象(在客戶端使用JS實現),然後將其發送給Ajax.NET方法(服務器端的C#代碼),服務器端的方法獲取DataSet中的XML字符串,並將其保存爲XML文件。

可以通過JS在客戶端獲取數據,然後將其保存到服務器端。

PS:該文章講解了客戶端如何獲取服務器端發送來的DataSet對象,以及客戶端如何創建DataSet對象並將其發送給服務器端。

當然,DataSet對象,無論是在客戶端還是在服務器端都是可以和其他數據格式,諸如XML、JSON、某個類的實例等進行轉換的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章