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:
- Use the constructor of the datatable class to create a datatable object. For example:
Datatable table = new datatable ();
- 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
- 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.