1.
2.
3. Example: in 100 cycles, perform the open and close of the database connection and use stopwatch to view the time spent.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingSystem.Diagnostics;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) {Stopwatch Watch=NewStopwatch (); Watch. Start (); for(inti =0; I < -; i++) {SqlConnection conn=NewSqlConnection ("server=.; Database=dbtest;uid=sa;pwd=123"); Conn. Open (); Conn. Close (); Conn. Dispose (); } watch. Stop (); Label1. Text=Watch. Elapsedmilliseconds.tostring (); } Private voidButton2_Click (Objectsender, EventArgs e) {Stopwatch Watch=NewStopwatch (); Watch. Start (); for(inti =0; I < -; i++) {SqlConnection conn=NewSqlConnection ("server=.; database=dbtest;uid=sa;pwd=123; Pooling=false"); Conn. Open (); Conn. Close (); Conn. Dispose (); } watch. Stop (); Label2. Text=Watch. Elapsedmilliseconds.tostring (); } }}
View Code
Conclusion:
1. Each application in ADO maintains a pool of connection objects, which is not created each time it is used, but is first fetched from the pool and then used directly.
New if not, reduces the time of each new build
2. Create a collection space in memory to store idle objects, if each time an object is needed, first obtained from the collection if it is not obtained in the new.
3. Object pooling: Improve execution efficiency
4 Description:
(1) Two connections The string used must be the same
(2) Open use, close () after the use of the connection object is placed in the connection pool, when using the Connection object, go to the connection pool to find, available to use.
(3) If a connection object is not closed in use and a connection object is required, a new connection object is recreated because there is no idle connection object at this time.
(4) The number of alternate connection objects can be set, set in the connection string.
(5) Close does not really close the connection, but instead puts the connection object into the connection pool.
ADO. NET Review Summary (2)--Connection pool