DataAdapter Detailed Introduction

Source: Internet
Author: User

First, the characteristics of the introduction

1. Represents a set of data commands and a database connection for populating the DataSet and updating the SQL Server database.

2, there is no direct connection between the SqlDataAdapter and the dataset. When the Sqldataadpater.fill (DataSet) call is complete, there is no connection between the two objects.

Second, the use of the 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 function

①string strconn= "provider= ...";

String Strsql= "select * from Customers";

SqlDataAdapter Da=new SqlDataAdapter (Strsql,strconn);

②string strconn= "provider= ...";

SqlConnection cn=new SqlConnection (strconn);

SqlDataAdapter Da=new SqlDataAdapter ("select * from Customers", CN);

③string strconn= "provider= ...";

String Strsql= "select * from Customers";

SqlConnection cn=new SqlConnection (strconn);

SqlCommand cmd=new SqlCommand (STRSQL,CN);

SqlDataAdapter Da=new SqlDataAdapter (CMD);

3, from the query to obtain results

① using the Fill method

......

SqlDataAdapter Da=new SqlDataAdapter (Strsql,strconn);

DataSet ds=new DataSet ();

Da. Fill (DS); Here, the table in the DS is named tables.

② 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);

③ using overloaded Fill method

Sqldataadapter.fill (DataSet, "Customers");

Sqldataadapter.fill (DataTable); [Page]

Sqldataadapter.fill (Dataset,intstartrecord,intnumrecords, "TableName");

④ Open and close connections

If a SqlDataAdapter object's Fill method is invoked, and the connection of the SelectCommand property is closed, SqlDataAdapter opens a connection, submits the query, gets the result, and finally closes the connection. If connection is opened before the call, then the operation remains open.

......

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 ();

⑤ call Fill method multiple times

The simplest solution for refreshing data in a dataset is to empty the dataset (or DataTable) and then call the Fill method of the DataAdapter object again.

Introduction of Property Method events

1, properties

①acceptchangeduringfill: Determines the RowState of the rows obtained by DataAdapter (the default is true).

②deletecommand: Gets or sets a Transact-SQL statement or stored procedure to delete records from the dataset.

③insertcommand: Gets or sets a Transact-SQL statement or stored procedure to insert a new record in the data source.

④selectcommand: Gets or sets a Transact-SQL statement or stored procedure that is used to select records in the data source.

⑤updatecommand: Gets or sets a Transact-SQL statement or stored procedure that updates the records in the data source.

⑥tablemappings:sqldataadapter A collection of information used to map the results of a query to a dataset.

⑦continueupdate: Controls whether the SqlDataAdapter continues to commit changes after an error has been encountered (default is False).

2. Methods

①fill: Executes queries stored in SelectCommand and stores the results in a DataTable.

②fillschema: Gets schema information for the query stored in SelectCommand. Gets the column names and data types in the query.

③getfillparameters: Gets an array containing the arguments for SelectCommand.

④update: Commits changes to a database that are stored in a dataset (or DataTable, DataRows). The method returns an integer value that contains the number of rows that were successfully updated in the data store.

3. Events

①fillerror: The event is triggered when DataAdapter encounters an error that fills a dataset or DataTable. [Page]

②rowupdated: Triggered when a modified row is submitted to the database.

③rowupdating: Triggered before submitting a modified row to the database.

==============================================

Dbdataadapter.fill method

Name Description

The Dbdataadapter.fill (dataset) Adds or refreshes rows in the dataset to match the rows in the data source using the dataset name, and creates a DataTable named "Table."

Dbdataadapter.fill (DataTable) Adds or refreshes rows in the specified range of the dataset to match the rows in the data source using the dataset, DataTable, and IDataReader names.

Dbdataadapter.fill (DataSet, String) adds or refreshes rows in the dataset to match the rows in the data source using the dataset and DataTable names.

Dbdataadapter.fill (DataTable, IDataReader) adds or refreshes rows in the dataset to match the rows in the data source using the dataset name, and creates a DataTable.

Dbdataadapter.fill (DataTable, IDbCommand, CommandBehavior) Add or Refresh rows in the DataTable to match the rows in the data source using the specified DataTable and IDataReader names.

Dbdataadapter.fill (Int32, Int32, datatable[]) Add or refresh rows in a DataTable to match the number of using the DataTable name, the specified SQL SELECT statement, and the CommandBehavior According to the line in the source.

Dbdataadapter.fill (Datasets, Int32, Int32, String) Add or refresh rows in the specified range of the dataset to match the rows in the data source using the dataset and DataTable names.

Dbdataadapter.fill (datatable[], IDataReader, Int32, Int32) Add or refresh rows in the dataset to match the rows in the data source using the dataset name, and create a DataTable.

Dbdataadapter.fill (DataSet, String, IDataReader, Int32, Int32) Add or refresh rows in the dataset to match the rows in the data source using the dataset name, and create a DataTable.

Dbdataadapter.fill (datatable[], Int32, Int32, IDbCommand, CommandBehavior) Add or refresh rows in the specified range of the dataset to match the use of the dataset and Datatab The row in the data source for the Le name.

Dbdataadapter.fill (DataSet, Int32, Int32, String, IDbCommand, CommandBehavior) using DataSet and source table names, command strings, and command behavior, in the dataset To add or refresh certain rows in a set range to match them to the corresponding rows in the data source.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.