DataSet, DataAdapter, and ADO. netdataadapter In ado. NET
DataSet and DataTable
DataSet is a data set stored in the memory. It is a temporary database.
In my opinion, it is not very common. DataTable or directly store data with List <model> is better than this.
Let's talk about the usage.
DataSet ds = new DataSet ("school"); DataTable dt = new DataTable ("stu"); dt. columns. Add ("column name", typeof (string ));
// Note that DataRow dr = new DataRow (); cannot be used like this. Why? See the green text below
/*
The DataRow constructor is protected internal and you cannot call it.
However, the DataTable of the same assembly as DataRow can be called.
*/
DataRow dr = dt. NewRow (); dr ["column name"] = "xxx"; dt. Rows. Add (dr); ds. Tables. Add (dt );
DataAdapter
This class is an application in the adapter mode. So what exactly does this adapter adapt?
DataAdapter is applicable to the DataTable and DataReader return values.
String conn = ""; // connection string SQL = ""; // SQL statement/* DataAdapter is the encapsulation of Connection, Command, and DataReader */DataAdapter da = new DataAdapter (SQL, conn); DataTable dt = new DataTable (); /****** description ******* 1. constructing columns and filling columns will improve the efficiency. 2. Fill (, dt); is a paging method suitable for small projects. Go to one page and read all! */Da. Fill (0, 10, dt );
DataReader is recommended
1. A common way to obtain tables
// For short, DataSet ds = new DataSet (); DataAdapter da = new DataAdapter (SQL statement, connection string) da. fill (ds); // In fact, it is not recommended to obtain tables in this way. DataTable dt = ds. Tables [0];
2. Recommended Practices
// You can also abbreviated using (Connection conn = new Connection ("Connection string") {using (Command cmd = conn. createCommand () {using (DataReader reader = cmd. executeReader () {DataTable dt = new DataTable (); dt. load (reader );}}}
Under what circumstances is DataSet used?
1.InIn C/S Mode
Use DataSet in C/S mode.
2.In B/S Mode
Use DataSet in B/S mode. In B/S mode (ASP. NET), DataSet is used, with 1000 requests,
There will be 1000 DataSet in the memory of the Web server. Whether to use it or choose between them.