Asp. NET hands-on Tutorials (one) __.net

Source: Internet
Author: User
Access to xml-based data
As mentioned at the beginning of this chapter, datasets are designed to abstract data without relying on the actual data source. You can understand this by changing the data in the instance from SQL to XML. The dataset supports the ReadXml method, which uses the FileStream object as its argument. In this case, the file you read must contain the (XML) plan and the data to be read. The dataset expects data to be used in the form, as shown in the following:
<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 single row/record in the table. The following example uses the ReadXml method of the dataset to read plans and data from an XML file. Note: When the data is read into the dataset, it does not differ from the SQL data---the datagrid binds it as if it were bound to SQL data, as in the following example:
C # datagrid17.aspx
[Run] | [Source code]
You can also read plans and data separately using the Readxmldata and ReadXmlSchema methods of the DataSet, as in the following example.
C # datagrid18.aspx
[Run] | [Source code]
In addition to supporting methods for reading XML data, the dataset also supports writing XML data. The following example implements a tool to query SQL data and write the results as XML data or planned text.
C # xmlgen.aspx
[Run] | [Source code]

Summary of this chapter
The common language Runtime (CLR) application interface that manages data access is a consistent way to extract and represent data without relying on actual data sources such as SQL Server, OLE DB, XML, and so on.

In order for the ASP.net page to access the SQL database, the System.Data and System.Data.SqlClient namespaces must be introduced into the page. If you are accessing the OLE DB database, you need to introduce the System.Data and System.Data.OleDb namespaces.

Assemble a DataSet (dataset) from a SQL query, including establishing a connection (SqlConnection), associating the SqlDataAdapter object containing the query statement, and then populating the dataset with the SqlDataAdapter Fill method (DataSet) Several steps.

The DataGrid control supports the DataSource property, which uses the IEnumerable or ICollection type. You can set these types to the results of the SQL query by assigning values to the dataset's DefaultView properties (Type DataView).

SqlDataAdapter contains a collection of parameters, you can replace the value with a variable marker (before the name with a "@").

When executing SQL commands that do not return results, such as inserts, updates, and deletes, you can use SqlCommand instead of SqlDataAdapter. Executes the command by calling ExecuteNonQuery, which returns the number of records that are in effect.

When using SqlCommand, you must explicitly open the SqlConnection (SqlDataAdapter Automatic Processing to open the connection). Remember to close the SqlConnection before you finish the page execution. Otherwise, the connection limit is unknowingly exhausted when the garbage collection function processes the page instance.

To allow records to be edited, the DataGrid supports an integer-type EditItemIndex attribute that indicates which row in the table is edited. When this property is set, the DataGrid displays the contents of the row in place of the text label using an input box, which can be edited.

The DataGrid provides the DataKeyField property to set the field name for the primary key. In UpdateCommand-bound event handling, you can get the name of the primary key from the Data key collection in the DataGrid.

Using the BoundColumn control in the DataGrid allows you to fully control the state of the column, including the ReadOnly property.

Using the TemplateColumn control in the DataGrid allows you to fully control the contents of the column

ButtonColumn (Button columns) can be used to provide a button control on each row of the column, and it can associate events.

A hyperlinkcolumn (hyperlink column) can be added to a column collection in the DataGrid, which can be directed to other pages when clicked.

When the allowsorting (allow sorting) property of the DataGrid is set to True, it provides a hyperlink at the column header position to activate the sort command and return to the table. When the user clicks on this (for sort) link, the invoked event handle can be set in the Onsortcommand property of the DataGrid.

The dataset supports the ReadXml, Readxmldata, and ReadXmlSchema methods, using FileStream as a parameter that can be used to assemble a dataset from an XML file.

Use stored procedures to reduce the load on the database in your application.

Because this part of the content is more, so we use three chapters, including 9, 10, 11, only asp.net server-side data access all finished. Start with the following chapters to explain data access and user ordering. Please look forward to ...

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.