ASP. NET getting started tutorial (11)

Source: Internet
Author: User
Access XML-based data
As mentioned at the beginning of this chapter, dataset is designed to abstract data without relying on actual data sources. You can understand this by changing the instance data from SQL to XML. Dataset supports the readxml method, which uses the filestream object as its parameter. In this case, the file you read must contain the (XML) plan and data to be read. The data that dataset expects to use in form, as shown below:
<Documentelement>
<Tablename>
<Columnname1> column value </columnname1>
<Columnname2> column value </columnname2>
<Columnname3> column value </columnname3>
<Columnname4> column value </columnname4>
</Tablename>
<Tablename>
<Columnname1> column value </columnname1>
<Columnname2> column value </columnname2>
<Columnname3> column value </columnname3>
<Columnname4> column value </columnname4>
</Tablename>
</Documentelement>
Each tablename segment corresponds to a row/record in the table. The following example uses the readxml method of dataset to read the plan and data from the XML file. Note: when the data is read into the dataset, it is no different from the SQL data. The DataGrid is bound to the data just as it is bound to the SQL data, as shown in the following example:
C # datagrid17.aspx
[Run] | [ Source code ]
You can also use the readxmldata and readxmlschema methods of dataset to read the plan and data respectively, as shown in the following example.
C # datagrid18.aspx
[Run] | [Source Code ]
In addition to reading XML data, dataset also supports writing XML data. The following example provides a tool to query SQL data and write the results as XML data or plan text.
C # xmlgen. aspx
[Run] | [Source Code]

Summary of this Chapter
Common Language Runtime (CLR) application for managing data accessProgramThe interface extracts and represents data in a consistent way without relying on actual data sources, such as SQL Server, oledb, and XML.

To enable the ASP. NET page to access the SQL database, the system. Data and system. Data. sqlclient namespaces must be introduced on the page. To access the oledb database, you must introduce the system. Data and system. Data. oledb namespaces.

Query and assemble a dataset (Dataset) from SQL, including establishing a connection, associating the sqldataadapter object containing the query statement, and then filling the dataset (Dataset) with the fill method of sqldataadapter.

The DataGrid Control supports the datasource attribute, which uses the ienumerable or icollection type. You can assign values to the defaultview attribute of dataset (whose type is dataview) to set these types to the SQL query results.

Sqldataadapter contains a set of parameters. You can replace the value with a variable identifier (Add "@" before the name "@").

When you execute an SQL command that does not return results, such as insert, update, or delete, you can use sqlcommand instead of sqldataadapter. Executenonquery is called to execute the command.

When using sqlcommand, you must explicitly enable sqlconnection (sqldataadapter automatically opens the connection ). Before the page is executed, make sure to disable sqlconnection. Otherwise, the connection restrictions will be exhausted unconsciously when the garbage collection function processes the page instance.

To allow the record to be edited, The DataGrid supports the edititemindex attribute of the integer type, which can indicate which row in the table is edited. After this attribute is set, the DataGrid uses an input box (which can be edited) to display the row content instead of text labels.

The DataGrid provides the datakeyfield attribute, which allows you to set the field name for the primary key. In event processing bound to updatecommand, you can obtain the primary key name from the data key set of the DataGrid.

Using the boundcolumn control in the DataGrid allows you to fully control the column status, including the readonly attribute.

The templatecolumn control in the DataGrid allows you to fully control the column content.

The buttoncolumn can be used to provide a button control for each row of the column, which can be associated with events.

Hyperlinkcolumn can be added to the column set of the DataGrid. When the link is clicked, it can be directed to other pages.

When the allowsorting attribute of the DataGrid is set to true, it provides a hyperlink in the column title position to activate the sort command and return it to the table. When you click this link, the called event handle can be set in the onsortcommand attribute of the DataGrid.

Dataset supports readxml, readxmldata, and readxmlschema methods. It uses filestream as a parameter and can be used to assemble dataset from XML files.

Using Stored Procedures can reduce the load on databases in applications.

As this part contains a lot of content, we use three chapters, including 9, 10, and 11, to complete the Asp.net server-side data access. The following sections describe data access and user customization. Coming soon...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.