Asp. NET database Programming (3)

Source: Internet
Author: User
Asp.net| Programming | data | Database display DataSet

We've got the data ready before. Let's take a look at how to display the data in the dataset. In ASP.net, the commonly used control for displaying a DataSet is a DataGrid, an HTML control in asp.net that can be well represented as a table, with arbitrary control over the appearance of the table and even pagination. Here we just need to use it simply:

<asp:datagrid id= "Datagridname" runat= "Server"/>

The rest of the task is to bind the DataSet to this DataGrid, binding is an important concept of asp.net, we will explain in another text. Generally, you need to bind a DataView to a DataGrid instead of binding the dataset directly. Fortunately, the dataset has a default DataView, and we bind it to the DataGrid:
Myfirstdatagrid.datasource = _
Objdataset.tables ("Author information"). DefaultView
Myfirstdatagrid.databind ()


Usage of the DataSet
The Dataset is not a simple replica of the recordset. In a certain sense, DataView is more like a recordset. If DataReader is the easiest way to access data, the dataset is the most complete data access object. With a dataset, you can manipulate existing data, create a dataset from a program, add a table to a dataset, and build relationships between the table.

Several steps to using a dataset

Step 1th, create a connection to the data source:

SqlConnection con =new SqlConnection ("server=localhost;uid=sa;pwd=;d atabase=pubs");

The 2nd step is to create a DataSetCommand object, specify the name of a stored procedure or an SQL statement, and specify the data link;

Sqldatasetcommand cmd =new sqldatasetcommand ("SELECT * from Authors", con);

Step 3rd, create a DataSet object

DataSet ds = new DataSet ();

The 4th step is to call the DataSetCommand Filldata method to populate the dataset with data. Note: The data link is not necessarily open. If the data link is off, the Filldata function opens it and closes the data link after filldata. If the data link is already open, the data link remains open after filldata.

int irowcount = cmd. FillDataSet (ds, "Authors");

The 5th step is to manipulate the data. Since Filldata returns the number of records, we can construct a loop to manipulate the data in the dataset.


for (int i=0; i< irowcount; i++) {
DataRow dr = ds. Tables[0]. Rows[i];
Console.WriteLine (dr["au_lname"]);
}

Data binding Technology

The Repeater, DataList, and DataGrid controls are several related page components in the System.Web.UI.WebControls name Space (Namespace). These controls represent the data that is bound to them through HTML, and they become "list-bound controls" (List-bound controls).

Like other Web Components, these components not only provide a consistent programming model, but also encapsulate the HTML logic associated with the browser version. This feature allows programmers to program on this object model without considering the differences and inconsistencies of various browser versions.

These three controls have the ability to "translate" their related data into various skins. These skins include a table, a multiple-column list, or any HTML stream. At the same time, they also allow you to create arbitrary display effects. In addition, they encapsulate the ability to handle submission data, state management, and event firing. Finally, they provide standard operations at various levels, including selection, editing, paging, sorting, and so on. With these controls, you can easily complete the following Web apps: reports, shopping carts, product listings, query results, navigation menus, and more.

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.