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、某个类的实例等进行转换的。

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