Ado. NET reading notes series of------SqlDataAdapter objects

Source: Internet
Author: User
Ado| Notes | objects
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);

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.

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

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


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.