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.