I. Principles of SqlDataAdapter and DateSet: DateSet is the memory resident representation of data. It provides a consistent relational programming model independent of the data source; to some extent, DateSet is an invisible database. But what really deals with the data source is SqlDataAdapter, including filling in the data source and updating the data source from the dataset. SqlDataAdapter uses the Fill () method to populate the retrieved data with DateSet. Process: IE -- GridView -- DataSet -- SqlDataAdapter -- sqldatabase public
Void BindGrid () { SqlDataAdapter myAdp =
New
SqlDataAdapter ("select * from UserAdmin order byUserId desc", conn ); DataSet ds = newDataSet (); MyAdp. Fill (ds,
"Authors "); // Label1.Text = ds. Tables [0]. Rows. Count. ToString ();//Total number of records obtained; GridView1.DataSource = ds. Tables ["Authors"]. DefaultView; GridView1.DataBind ();} II. sqlcommand and sqldatareade principles: sqlcommand uses the executereader () method to send the obtained data to the sqldatareade object. Sqldatareade puts the data obtained from the data source into the buffer for processing on a row-by-row basis. Advantage: sqldatareade runs fast and improves application performance. Advantage: once the data is available, sqldatareade immediately retrieves the data, instead of waiting for all the query results to be returned. By default, only one row is stored in the buffer zone at a time, reducing system overhead. Disadvantage: inflexible use. Process: IE -- sqldatareade -- sqlcommand -- sqldatabase Note: after each sqldatareade object is used, you must call the close () method to close it. Because the sqldatareade object uses connection exclusively. Example: Sqlcommandmyconn =
Newsqlcommand ("select * From v_economical my2_comiddwhere eid ="
+ Request. querystring ["Eid"] +
"", Conn ); Conn. open (); SqlDataReader rd = myconn. ExecuteReader (); Rd. Read (); Lbyear1.Text = rd ["year1"]. ToString (); Lbmonth1.Text = rd ["month1"]. ToString (); Lbcom_name.Text = rd ["com_name"]. ToString (); Rd. Close ();
Conn. Close ();
The difference is:
SqlDataAdapter:
The "group" Data command used to fill the DataSet and update the SQL database is connected to the "one" database.
SqlDataAdapter can operate not only Multiple SQL commands, but also one SQL command
DataSet is the abstraction of relational databases, ADO. NET uses DataAdapter (data adapter) as a bridge between dataset and the data source. DataAdapter provides the Fill () method to obtain data from the database and generate DataSet.
SqlCommand:
The "one" SQL statement or stored procedure executed on the SQL database.
SqlCommand can operate only one SQL command
I personally think SqlCommand is suitable for updating a small amount of data. It is fast and easy to use. It executes SQL statement queries.
Sqldataadapter is used to put data into dataset. After the data is modified and deleted in a centralized manner, the entire dataset is submitted back to the database for processing through update.