Ado.net ~~ (Acceleration)

Source: Internet
Author: User
Tags connection reset connectionstrings

Of course, the first step is to use the link pool ~~
Connection Pool
Connection timeout -- the default waiting time for trying to connect to the data storage zone is 15 seconds.
Min pool size-minimum capacity of the Connection Pool
Max pool size-the maximum capacity of the connection pool is 100 by default.
If pooling is true by default, a new connection is returned from the connection pool.

Connection Reset indicates that the connection will be reset when the database connection is deleted from the connection pool. The default value is true, if this parameter is set to false, the number of requests to and from the server during connection creation is less, but the connection status is not updated.

If something goes wrong ~~ Sqlconnection. clearallpools (); // clear the connection pool
------ Next is the major drama ~~ Naturally, it uses Asynchronization.
1 first, set an async = true in the connection string
------- There are so many theories ~~ Easy to read code


31041 records, 4 seconds


Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class default5: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{


}
Protected void button#click (Object sender, eventargs E)
{
Datetime old = datetime. now;

Sqlconnection dbcon;
Sqlcommand command = new sqlcommand ();
Sqldatareader ordersreader;
Iasyncresult asyncresult; // asynchronous


Dbcon = new sqlconnection ();
Dbcon. connectionstring = system. configuration. configurationmanager. connectionstrings ["connectionstringinfo"]. connectionstring;
Command. Connection = dbcon;


Command. commandtext = "select ";
Command. commandtype = commandtype. storedprocedure;
Command. Connection = dbcon;

Try
{
Dbcon. open ();
Asyncresult = command. beginexecutereader ();
While (! Asyncresult. iscompleted) // indicates whether the asynchronous operation has been completed.
{
// Because the asynchronous operation must stop the thread for seconds
System. Threading. thread. Sleep (10 );

}
Ordersreader = command. endexecutereader (asyncresult );
Gridview1.datasource = ordersreader;
Gridview1.databind ();
}
Catch (system. Exception)
{

}
Timespan not = datetime. Now-old;
Label1.text = Not. Seconds. tostring ();
}
}

-The above is just a small task ~~ Faster

// The most powerful wait call is to replace system. Threading. waithandle. waitall with system. Threading. waithandle. waitany because system. Threading. waithandle. waitany
// It can be processed after a certain process ends. Modify the try part.
Protected void button4_click (Object sender, eventargs E)
{
Datetime old = datetime. now;
// Actually, the first result set is the source of the search. The second result set does not search for any fields in the first result set, instead, use the first result set.
Sqlconnection dbcon = new sqlconnection (system. configuration. configurationmanager. connectionstrings ["connectionstringinfo"]. connectionstring );
Sqlcommand table_1command = new sqlcommand ("select * From table_2 where ID> 4000001", dbcon); // --- after the query is executed here
Sqlcommand mmcommand = new sqlcommand ("select title, content from mm, table_2 where mm. ID = table_2.id", dbcon); // table_2.id is actually a column in table_2 above

Table_1command.commandtype = commandtype. text;
Mmcommand. commandtype = commandtype. text;

Sqldatareader table_1datareader;
Sqldatareader mmdatareader;

Iasyncresult table_1asyncresult;
Iasyncresult mmasyncresult;

System. Threading. waithandle [] whandles = new system. Threading. waithandle [2];

// Encapsulate the OS-specific objects waiting for exclusive access to shared resources.
System. Threading. waithandle table_1whandle;
System. Threading. waithandle mmwhandle;

Try
{

Dbcon. open ();
Table_1asyncresult = table_1command.beginexecutereader ();
Mmasyncresult = mmcommand. beginexecutereader ();

Table_1whandle = table_1asyncresult.asyncwaithandle;
Mmwhandle = mmasyncresult. asyncwaithandle;

Whandles [0] = table_1whandle;
Whandles [1] = mmwhandle;

System. Threading. waithandle. waitany (whandles );





For (INT Index = 0; index <2; index ++)
{
// -------- Return complete execution wait for the handle to index A data in the whandles Index
Int whindex = system. Threading. waithandle. waitany (whandles );
Switch (whindex)
{
// Note that the index of the whandles set must be the same here
Case 0:
Table_1datareader = table_1command.endexecutereader (table_1asyncresult );
Gridview1.datasource = table_1datareader;
Gridview1.databind ();
Break;
Case 1:

Mmdatareader = mmcommand. endexecutereader (mmasyncresult );
Gridview2.datasource = mmdatareader;
Gridview2.databind ();

Break;

}

}








}
Catch (system. Exception)
{

}
Finally
{
Dbcon. Close ();
}



Timespan not = datetime. Now-old;
Label1.text = Not. Seconds. tostring ();
}

~~ The above is an advanced application-but how to speed up security first
First, you must set up three servers ~~ Or three sqlserver instances.

The main server is.
The scene server is. \ partner.
The observer server is. \ witness.

Then set failover parter = ". \ partner" in the connection string.

-- When data is inserted to the master server, data is inserted to the image server. If the master server stops working, the image server is set as the master server by the observer server.

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.