SqlDataAdapter Brief Introduction (Turn)

Source: Internet
Author: User

from:http://blog.sobnb.com/u/92/5532.html

First, the characteristics of the introduction

1. Represents a set of data commands and a database connection for populating a dataset and updating a SQL Server database.
2. There is no direct connection between the SqlDataAdapter and the dataset. When a Sqldataadpater.fill (DataSet) call is completed, there is no connection between the two objects.

******

SqlDataAdapter does not need to have an active SqlConnection object before the Fill method call, SqlDataAdapter opens the database in the STRCONN statement itself, and after obtaining the query results, closes the connection to the database. If the SqlConnection object already exists, the SqlConnection object is returned to its original state, whether or not it is already open, SqlDataAdapter executes the Fill method.
When multiple SqlDataAdapter objects in a program use both a SqlConnection object, to avoid opening and closing SqlConnection objects multiple times, The connection to the database should be opened by calling SqlConnection's Open method before calling the SqlDataAdapter fill method, and then calling the Close method of SqlConnection to close the database connection after the fill call is completed.

******

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. Get results from queries
① using the Fill method
...
SqlDataAdapter Da=new SqlDataAdapter (Strsql,strconn);
DataSet ds=new DataSet ();
Da. Fill (DS); The table in DS here is named tables
② using the Fill method to create a DataTable object and a DataColumn object
...
SqlDataAdapter Da=new SqlDataAdapter (Strsql,strconn);

Let DS. Tables[0]. TableName to Customers

Da. Tablemapping.add ("Table", "Customers");

DataSet ds=new DataSet ();
Da. Fill (DS);
③ using the 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 called, and the connection of the SelectCommand property is closed, SqlDataAdapter will open a connection and then commit the query, fetch the result, and finally close the connection. If connection is opened before the call, it remains open after the 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 ();
⑤ multiple calls to the Fill method
The simplest solution to refreshing the data in a dataset is to empty the dataset (or DataTable) and then call the Fill method of the DataAdapter object again.

Iii. Description of Attribute method events
1. Properties
①acceptchangeduringfill: Determines the RowState of the Rows fetched by DataAdapter (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 records in the data source.
⑥tablemappings:sqldataadapter is used to map the results of a query to a collection of information for a dataset.
⑦continueupdate: Controls whether the SqlDataAdapter continues to commit changes after encountering an error (default is False).

2. Methods
①fill: Executes the query stored in SelectCommand and stores the result in a DataTable.
②fillschema: Gets schema information for queries stored in SelectCommand. Gets the column names and data types in the query.
③getfillparameters: Gets an array containing parameters for SelectCommand.
④update: Submits changes to the 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 populating the DataSet or DataTable.
②rowupdated: is triggered after a modified row is submitted to the database.
③rowupdating: is triggered before a modified row is submitted to the database.

SqlDataAdapter Brief Introduction (Turn)

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.