Datatable of ADO. Net object

Source: Internet
Author: User

 

Ado. Net can process data through dataset or datatable objects when disconnected from the database. It can connect to the data source again and update the data source only when data needs to be updated.

 

A datatable object is a table stored in the local memory. It provides various operations on the row and column data objects in the table. You can directly populate the data from the database to the able object, or add the datatable object to the existing DataSet object.

First, let's take a look at the basic structure of ADO. net.

 

The following describes how to create a able object.

 

1.Create a able object in either of the following ways:

 

  1. Use the constructor of the datatable class to create a datatable object. For example:

Datatable table = new datatable ();

  1. Call the add method of the tables object of dataset to create a datatable object.

Dataset dataset = new dataset ();

Datatable table = dataset. Tables. Add ("mytablename ");

2. Add columns to the datatable object

 

Call the add method in the column of the datatable object to add a column. For example:

Datatable table = new datatable ("Table1 ");

Table. Columns. Add ("name", typeof (system. Data. sqltypes. sqlstring ));

Table. Columns. Add ("Age", typeof (system. Data. sqltypes. sqlint32 ));

Note: Some data types in the SQL Server database (such as sqldatetime, sqldecimal, and sqlstring) are different from those in the Common Language Runtime Library (CLR, to save the created table to the SQL Server database, use system. data. the SQL Server data type provided in the sqltype namespace.

 

3. Create a line in the datatable object

 

Since each row of the datatable object is a datarow object, you can use the newrow method of the datatable object to create a datarow object and set the data of each column in the new row, then, add the datarow object to the table using the add method. For example:

// DT is a datatable object.

Datarow ROW = DT. newrow ();

Row ["name"] = "James ";

Row ["Age"] = 20;

DT. Rows. Add (ROW );

 

4. Fill the tables in the SQL Server database into the able object

 

Use the fill method of the dataadapter object.

String connectionstring = properties. settings. Default. mydatabaseconnectionstring;

Sqlconnection conn = new sqlconnection (connectionstring );

Sqldataadapter adapter = new sqldataadapter ("select * From mytable2", Conn );

Dataset dataset = new dataset ();

// If no table name is specified, the system automatically uses the default table name.

Adapter. Fill (Dataset );

// The table generated using the index

Datagridview1.datasource = dataset. Tables [0];

 

 

DataSet object

  1. Create a DataSet object

    • [Solution Resource Manager] --> Add a new or existing database
    • [Data] --> [create a data source] --> Create or add an existing database
    • When binding a form control, use the Wizard to create a dataset pair

Dataset mydatabase = new dataset ();

 

Fill in DataSet object

 

After creating a dataset, you can use the sqldataadapter object to import data to the DataSet object. For example, you can use the fill method to populate a table in the dataset.

 

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.