I. Features
1. Indicates a set of data commands for populating the DataSet and updating the SQL Server database and a database connection.
2, there is no direct connection between SqlDataAdapter and DataSet. When completing the SqlDataAdPater.Fill (DataSet) call, there is no connection between the two objects.
Second, use introduction
1. Create SqlDataAdapter
...
String strsql = "SELECT * from Customers";
SQLCommand cmd = new SQLCOMMAND (strsql, cn);
Sqldataadapter Da = new SqlDataAdapter ();
Da.selectCommand = CMD;
2, SQLDataAdapter constructor
1String strconn = "provider = .....";
String strsql = "SELECT * from Customers";
SqlDataAdapter Da = New SqlDataAdapter (strsql, strconn);
2String strconn = "provider = .....";
SqlConnection CN = New SqlConnection (STRCONN);
SqlDataAdapter Da = New SqlDataAdapter ("Select * from Customers", CN);
3String strconn = "provider = .....";
String strsql = "SELECT * from Customers";
SqlConnection CN = New SqlConnection (STRCONN);
SQLCommand cmd = new SQLCOMMAND (strsql, cn);
SqlDataAdapter Da = New SqlDataAdapter (CMD);
3, get the result from the query
1 Using a Fill method
...
SqlDataAdapter Da = New SqlDataAdapter (strsql, strconn);
DataSet DS = New DataSet ();
Da.fill (DS); // This is a table name in DS TABLE
2 Use the Fill method to create a DataTable object and a Datacolumn object
...
SqlDataAdapter Da = New SqlDataAdapter (strsql, strconn);
Da.TableMapping.Add ("Table", "Customers");
DataSet DS = New DataSet ();
Da.fill (DS);
3 use heavy-duty Fill method
SqlDataAdapter.Fill (Dataset, "Customers");
SqlDataAdapter.Fill (DataTable);
SqlDataAdapter.Fill (Dataset, IntStartRecord, INTNUMRECORDS, "TABLENAME");
4 open and close connections
If a SqlDataAdapter object's Fill method is called, while the connection of the SelectCommand property is closed, then SqlDataAdapter opens a connection, then submit the query, get the result, and finally close the connection. If you open a Connection before calling, it remains open after operation.
...
SqldataAdapter Dacustomers, Daorders;
Dacustomers = New SqldataAdapter ("Select * from Customers", CN);
Daorders = New SqldataAdapter ("Select * from Orders", CN);
DataSet DS = New DataSet ();
Cn.open ();
Dacustomers.Fill (DS);
Daorders.Fill (DS);
Cn.close ();
5 multiple calls Fill method
Refresh the data in the DataSet, the simplest solution is to empty the DataSet (or DataTable) and then call the Fill method of the DataAdapter object again.
Third, the attribute method introduction
1, attribute
1ACCEPTCHANGEDURINGFILL: Determines the row of the rows obtained by DataAdapter (default to true).
2DeleteCommand: Get or set a Transact-SQL statement or stored procedure to delete records from the dataset.
3INSERTCOMMAND: Get or set a Transact-SQL statement or stored procedure to insert a new record in the data source.
4selectCommand: Get or set a Transact-SQL statement or stored procedure for selecting records in the data source.
5UpdateCommand: Gets or sets a Transact-SQL statement or stored procedure for updating the records in the data source.
6TableMappings: SqlDataAdapter is used to map the results of the query to the information collection of the DataSet.
7ContinueUpdate: Control SqlDataAdapter does not continue to submit changes after encounter an error (default is false).
2, method
1FILL: Performs the query stored in SelectCommand and stores the result in the DataTable.
2FILLSCHEMA: Get schema information for query stored in SelectCommand. Get the column and data types in the query.
3GetfillParameters: Gets an array containing parameters for SELECTCOMMAND.
4Update: Submit the changes stored in DataSet (or DataTable, DataRows). This method returns an integer value in which the number of rows successfully updated in the data store.
3, event
1FLLERROR: This event is triggered when DataAdapter encounters an error in the DataSet or DataTable.
2RowUpdated: Triggered after submitting a modified row to the database.
3RowUpdating: Submit a modified row to the database.