SqlDataAdapter object


https://blog.csdn.net/qq_39657909/article/details/80615465

First, the features introduced
1, showing a set of commands and data and updating a database population DataSet SQL Server database connection.
2, between the DataSet SqlDataAdapter and not directly connected. Upon completion SqlDataAdpater.Fill (DataSet) call, between two objects not connected.
Second, the use Description

1. Create a SqlDataAdapter

  1. string strSQL=“Select * from Customers”;
  2.     SqlCommand cmd=new SqlCommand(strSQL,cn);
  3.     SqlDataAdapter da=new SqlDataAdapter();
  4.     da.SelectCommand=cmd;

2, SqlDataAdapter constructor

①string strConn=“Provider=.....”;

  1. string strSQL=“select * from Customers”;
  2.   SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);

②string strConn=“Provider=.....”;

  1. SqlConnection cn=new SqlConnection(strConn);
  2.   SqlDataAdapter da=new SqlDataAdapter(“select * from Customers”,cn);

③string strConn=“Provider=.....”;

  1. string strSQL=“select * from Customers”;
  2.   SqlConnection cn=new SqlConnection(strConn);
  3.   SqlCommand cmd=new SqlCommand(strSQL,cn);
  4.   SqlDataAdapter da=new SqlDataAdapter(cmd);

3, get the results from a query

① using the Fill method

  1. SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);
  2.     DataSet ds=new DataSet();
  3.     da.Fill (ds);   // where ds in the table named Table

② create DataTable objects and DataColumn objects using the Fill method

  1. SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);
  2.     da.TableMapping.Add(“Table”,“Customers”);   
  3.     DataSet ds=new DataSet();
  4.     da.Fill(ds);

③ using Fill Method overloading

  1.  SqlDataAdapter.Fill(DataSet,“Customers”);
  2.     SqlDataAdapter.Fill(DataTable);
  3.     SqlDataAdapter.Fill(DataSet,intStartRecord,intNumRecords,“TableName”);

④ open and close the connection

    If you call the Fill method of the SqlDataAdapter object, and Connection SelectCommand property closes, SqlDataAdapter will open a connection, and then submit a query, get the results, and finally close the connection. If you open the Connection before calling, then after the operation remains open.

  1. SqlDataAdapter daCustomers,daOrders;
  2.     daCustomers=new SqlDataAdapter(“Select * from Customers”,cn);
  3.     daOrders=new SqlDataAdapter(“Select * from Orders”,cn);
  4.     DataSet ds=new DataSet();
  5.     cn.Open();
  6.     daCustomers.Fill(ds);
  7.     daOrders.Fill(ds);
  8.     cn.Close();

⑤ Fill method called multiple times
    to refresh the data in the DataSet, the easiest solution is to empty DataSet (or a DataTable), then call the Fill method of the DataAdapter object again.

Third, the method attribute describes the events 
1, property
①AcceptChangeDuringFill: determine RowState line by DataAdapter acquired (the default is True).
②DeleteCommand: Gets or sets a Transact-SQL statement or stored procedure to delete records from the data set.
③InsertCommand: Gets or sets Transact-SQL statement or a stored procedure to insert a new record in the data source.
④SelectCommand: Gets or sets a Transact-SQL statements or stored procedures for selecting records in the data source.
⑤UpdateCommand: Gets or sets a Transact-SQL statements or stored procedures for updating records in the data source.
⑥TableMappings: SqlDataAdapter to the results of the query are mapped to the information collection of the DataSet.
⑦ContinueUpdate: SqlDataAdapter control whether to continue to commit the changes after encountering an error (default is false).
2. The method
①Fill: executes a program stored in the SelectCommand query, and the result is stored in a DataTable.
②FillSchema: get query schema information stored in the memory of the SelectCommand. Get each column name and data type in a query.
③GetFillParameters: to get a SelectCommand contains an array of parameters.
④Update: commit the changes stored in the DataSet (or a DataTable, DataRows) in the database. This method returns an integer value, which contains the number of rows in the data store successfully updated.
3, the event
①FillError: When an error is encountered DataAdapter to fill a DataSet or DataTable, and the event is triggered.
It triggered submit a modified row to the database after: ②RowUpdated.

③RowUpdating: triggered before submitting a modified row to the database.

 

Command of the difference between: https: //blog.csdn.net/qq_39657909/article/details/80615355

Reference: https: //blog.csdn.net/chenjinglong/article/details/12291147

 

Guess you like

Origin www.cnblogs.com/wfy680/p/12004532.html