C # DataSet and DataTable

Source: Internet
Author: User

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?
    1. SqlDataAdapter da=New SqlDataAdapter (CMD);
    2. DataTable dt=New DataTable ();
    3. Da. Fill (DT);

Put the data results directly into the DataTable,

DEMO2:

[CSharp]View Plaincopy print?
    1. SqlDataAdapter da=New SqlDataAdapter (CMD);
    2. DataSet dt=new DataSet ();
    3. Da. Fill (DT);

Data results in a dataset, to use that DataTable, you can: Dataset[0]
More common usage:

[CSharp]View Plaincopy print?
    1. SqlDataAdapter da=New SqlDataAdapter (CMD);
    2. DataSet dt=new DataSet ();
    3. Da.    Fill (DT,"table1");

When used: This takes datatable:dataset["table1"]

Or:

[CSharp]View Plaincopy print?
    1. SqlDataAdapter adapter = New SqlDataAdapter (strSQL, _connection);
    2. Adapter. Fill (DataSet);
    3. 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?
    1. DataTable table = db. Getdatatable (strSQL);
    2. 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?
    1. foreach (DataRow row in dt. Rows)
    2. {
    3. foreach (DataColumn column in dt. Columns)
    4. {
    5. Console.WriteLine (Row[column]);
    6. }
    7. }

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?
    1. StringBuilder maillist = new StringBuilder ();
    2. foreach (DataRow row in dt. Rows)
    3. {
    4. Maillist.append (row["Email"]);
    5. Maillist.append (";");
    6. }

Manually add row content to a DataTable and bind, update, sort

[CSharp]View Plaincopy print?
  1. DataTable dt = new DataTable ();
  2. DataRow Dr;
  3. Dt.  Columns.Add (new DataColumn ( "integer value", typeof (Int32)));
  4. Dt.  Columns.Add (new DataColumn ( "String value", typeof (String)));
  5. Dt.  Columns.Add (new DataColumn ( "datetime value", typeof (DateTime)));
  6. Dt.  Columns.Add (new DataColumn ( "boolean", typeof (bool)));
  7. for (int i = 1;   I <= 9; i++) {
  8. Dr = dt. NewRow ();
  9. Dr[0] = i;
  10. DR[1] = "item" + i.tostring ();
  11. DR[2] = DateTime.Now;
  12. DR[3] = (i% 2 = 0)?  true: false;
  13. Dt. Rows.Add (DR);
  14. }
  15. DataGrid1.DataSource = new DataView (DT);
  16. Datagrid1.databind ();

More operations:
1. Creating tables and inserting columns

[CSharp]View Plaincopy print?
    1. DataTable dt=New DataTable ();
    2. Dt.  Colunmns.add ("Name", System.Type.GetType ("System.String"));


2. Insert Row

[CSharp]View Plaincopy print?
    1. DataRow newrow = dt. NewRow ();
    2. newrow[0]="AA";
    3. Dt. Rows.Add (NewRow);

3. Sorting

[CSharp]View Plaincopy print?
    1. DataView dv = dt. DefaultView;
    2. Dv.   Sort = "Name Desc";
    3. DataTable dt1 = dv. ToTable ();

4. Insert a row at the specified location

[CSharp]View Plaincopy print?
  1. DataTable dt = new DataTable ("table name");
  2. DataRow dr = dt. NewRow ();
  3. Dr[0]="An Giang Union outdoor mold";
  4. DR[1] = "555";
  5. DR[2] = "200707";
  6. DR[3] = "0.00";
  7. DR[4] = "0.00";
  8. DR[5] = "0.00";
  9. DR[6] = "0.00";
  10. Dt.   Rows.Add (DR); //In the last insert row
  11. Dtb1. Rows.insertat (DR,J);  //j Inserts the position of the specified row;
  12. Datagridview1.datasource = DT;

Transferred from: http://blog.csdn.net/simaweier/article/details/8443293

C # DataSet and DataTable

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.