How to use DataTable DataSet DataView in. net

Source: Internet
Author: User

This article mainly introduces DataTable, DataSet usage, and attribute summary. Therefore, only focus is thrown. For DataTable, DataSet briefly describes that dataset is a DataSet that can contain datatable and the relationship between data tables. datatable is a data table, including columns and row rows. DataView is a view used to create different views of the data stored in the DataTable. It is usually used for data binding. The method is as follows:

The code is as follows: Copy code

DataView DV = ds. Tables ["table name"]. DefaultView;
DataGrid1.DataSource = DV;

Generally speaking, DataSet is a DataSet. You can include multiple datatables, such as dataset ["table name"] to get the DataTable; DataTable: data table. Example 1:

The code is as follows: Copy code

SqlDataAdapter da = new SqlDataAdapter (cmd );
DataTable dt = new DataTable ();
Da. Fill (dt );

Directly put the data results in the datatable. Example 2:

The code is as follows: Copy code

SqlDataAdapter da = new SqlDataAdapter (cmd );
DataSet dt = new DataSet ();
Da. Fill (dt );

Put the data results in dataset. If you want to use the datatable, you can do this: dataset [0]. The following usage is quite common:

The code is as follows: Copy code

SqlDataAdapter da = new SqlDataAdapter (cmd );
DataSet dt = new DataSet ();
Da. Fill (dt, "table1 ");
// When used: Get the datatable as follows:
Dataset ["table1"]

In addition, the usage of online collection is as follows:

The code is as follows: Copy code

DataTable indicates a table in memory.
Create a able
DataTable dt = new DataTable ("tbAmount"); // Create a DataTable
Dt. Columns. Add ("riqi", System. Type. GetType ("System. String"); // Create a able column framework
// You can also use DataColumn dc = new DataColumn ("riqi", System. Type. GetType ("System. String"); dt. Columns. Add (dc );
Dt. Columns. Add ("bianm", System. Type. GetType ("System. String "));
Dt. Columns. Add ("xAmount", System. Type. GetType ("System. String "));
Dt. Columns. Add ("jPoints", System. Type. GetType ("System. String "));
DataRow drw = dt. NewRow (); // create a row
Drw ["riqi"] = "2008-12-25"; // add data
Drw ["bianm"] = "100007 ";
Drw ["xAmount"] = "234 ";
Drw ["jPoints"] = "900 ";
Dt. Rows. Add (drw); // Add the row to the able.

Select the best option. Each method has its own characteristics and is also quite common. Let's take a look.

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.