Overview
You can think of a DataTable and a dataset as a data container, such as when you query a database and get some results that you can put into a container, you might want to ask: I do not use this container, I read the variable or array of the same can be saved up ah, why the container?
The reason is that this kind of container is more powerful and can be used in addition to saving data. Example: In a C/s structure of the desktop database system, you can store the query results in front of the container data display to your client interface, the user in the interface to add, delete, modify the user's operations can be updated to the container, and so the user operation is complete, the request for updates, And then you update the container's entire data changes to the central database, what are the benefits? is to reduce the database operation, the client speed increased, the database pressure is reduced.
A dataset can be likened to an in-memory database, a DataTable is an in-memory data table, and a dataset can store multiple DataTable.
DataSet: DataSet. Usually contains multiple DataTable, when used, dataset["table name"] Get DataTable
DataTable: Data table.
DEMO1:
[CSharp]View Plaincopy print?
- SqlDataAdapter da=New SqlDataAdapter (CMD);
- DataTable dt=New DataTable ();
- Da. Fill (DT);
Put the data results directly into the DataTable,
DEMO2:
[CSharp]View Plaincopy print?
- SqlDataAdapter da=New SqlDataAdapter (CMD);
- DataSet dt=new DataSet ();
- Da. Fill (DT);
Data results in a dataset, to use that DataTable, you can: Dataset[0]
More common usage:
[CSharp]View Plaincopy print?
- SqlDataAdapter da=New SqlDataAdapter (CMD);
- DataSet dt=new DataSet ();
- Da. Fill (DT,"table1");
When used: This takes datatable:dataset["table1"]
Or:
[CSharp]View Plaincopy print?
- SqlDataAdapter adapter = New SqlDataAdapter (strSQL, _connection);
- Adapter. Fill (DataSet);
- return Dataset.tables[0]
When adapter. Fill (dataSet); After execution, the program returns a result set in memory through the SQL engine, which is a DataTable, and the DataTable is added home to Dataset.tables, so return Dataset.tables[0], which is to return the result set you want to query
Receive return:
[CSharp]View Plaincopy print?
- DataTable table = db. Getdatatable (strSQL);
- string rid = table. rows[0]["rid"]. ToString ();
Table. Rows[0] Represents the first row of table data, table. rows[0]["RID"]. ToString () represents the value of the column RID in table, and all columns in the table are all the columns you query in SQL
Traversing a DataTable:
From the general class, return a DataTable, to display each cell, just do two loops:
[CSharp]View Plaincopy print?
- foreach (DataRow row in dt. Rows)
- {
- foreach (DataColumn column in dt. Columns)
- {
- Console.WriteLine (Row[column]);
- }
- }
Column in Row[column] is the name of the table that is retrieved.
If you want to stitch a column's value into a string, remove the inner loop:
[CSharp]View Plaincopy print?
- StringBuilder maillist = new StringBuilder ();
- foreach (DataRow row in dt. Rows)
- {
- Maillist.append (row["Email"]);
- Maillist.append (";");
- }
Manually add row content to a DataTable and bind, update, sort
[CSharp]View Plaincopy print?
- DataTable dt = new DataTable ();
- DataRow Dr;
- Dt. Columns.Add (new DataColumn ( "integer value", typeof (Int32)));
- Dt. Columns.Add (new DataColumn ( "String value", typeof (String)));
- Dt. Columns.Add (new DataColumn ( "datetime value", typeof (DateTime)));
- Dt. Columns.Add (new DataColumn ( "boolean", typeof (bool)));
- for (int i = 1; I <= 9; i++) {
- Dr = dt. NewRow ();
- Dr[0] = i;
- DR[1] = "item" + i.tostring ();
- DR[2] = DateTime.Now;
- DR[3] = (i% 2 = 0)? true: false;
- Dt. Rows.Add (DR);
- }
- DataGrid1.DataSource = new DataView (DT);
- Datagrid1.databind ();
More operations:
1. Creating tables and inserting columns
[CSharp]View Plaincopy print?
- DataTable dt=New DataTable ();
- Dt. Colunmns.add ("Name", System.Type.GetType ("System.String"));
2. Insert Row
[CSharp]View Plaincopy print?
- DataRow newrow = dt. NewRow ();
- newrow[0]="AA";
- Dt. Rows.Add (NewRow);
3. Sorting
[CSharp]View Plaincopy print?
- DataView dv = dt. DefaultView;
- Dv. Sort = "Name Desc";
- DataTable dt1 = dv. ToTable ();
4. Insert a row at the specified location
[CSharp]View Plaincopy print?
- DataTable dt = new DataTable ("table name");
- DataRow dr = dt. NewRow ();
- Dr[0]="An Giang Union outdoor mold";
- DR[1] = "555";
- DR[2] = "200707";
- DR[3] = "0.00";
- DR[4] = "0.00";
- DR[5] = "0.00";
- DR[6] = "0.00";
- Dt. Rows.Add (DR); //In the last insert row
- Dtb1. Rows.insertat (DR,J); //j Inserts the position of the specified row;
- Datagridview1.datasource = DT;
Transferred from: http://blog.csdn.net/simaweier/article/details/8443293
C # DataSet and DataTable