Does connectiontimeout take 30 seconds? Connect to the database with multiple threads and display the connection Animation

Source: Internet
Author: User

I have been busy with EASYCODE recently. net code generator 2.20 upgraded version development work, two days ago, a friend asked me EASYCODE Connection database server, why can I return the database can be linked to the information within five seconds, instead of writing it as yourselfProgramIt will wait for 30 seconds like a "false death". The principle is actually very simple:

1. sqlconnection returns an error message quickly when the user name or password is incorrect. However, if the host name or IP address is incorrect due to network access, it does not work even if connectiontimeout is set.

2. If the connection is accessing a non-existent server, the progress will always wait for the result of the connection. open () method. For winform, the interface will be suspended.

 

Therefore, the solution is very simple:

1. Add an animation window to display the link database animation, and add the formclosing event processing method so that it cannot be closed with ALT + F4.

2. Start a thread to connect to the database and determine the thread execution time. If the thread execution time exceeds the specified time (for example, 5 seconds), the database cannot be connected.

3. Normally, the database can be connected very quickly. To avoid a flash in the animation window, you can set the shortest time (such as 0.5 seconds) for an animation window ).

 

I used EASYCODE as an example,CodeAnd the procedure is as follows:

 

In the connection window, the icon on the left is a GIF Animated Image:

 

the multi-threaded database connection code is as follows:

Private string connnectionstring; private bool connsuccess; private void btntestconn_click (Object sender, eventargs e) {connnectionstring = txtconnstr. text; connsuccess = false; testconnection (500,300 0); // multi-threaded connection to the database. The animation window is displayed at least 500 milliseconds and the timeout time is 3000 milliseconds.} public void testconnection (INT mintimes, int maxtimes) {formconnserver = new formconnserver (); formconnserver. show (); formconnserver. canclose = False; thread makeconnectionthread = new thread (testsqlserverconn); makeconnectionthread. isbackground = true; makeconnectionthread. Priority = threadpriority. Highest; makeconnectionthread. Start (); int sleeptimes = 0; while (! Connsuccess | sleeptimes <mintimes) {application. doevents (); thread. sleep (50); sleeptimes + = 50; If (sleeptimes> maxtimes) {makeconnectionthread. abort (); If (! Connsuccess) MessageBox. Show ("cannot establish a connection with the database server. Please confirm that the configuration information is correct. "," System prompt ", messageboxbuttons. OK, messageboxicon. information); formconnserver. canclose = true; formconnserver. close (); Return ;}} formconnserver. canclose = true; formconnserver. close (); MessageBox. show ("the connection to the database server is established successfully. ". Padright (50, '')," system prompt ", messageboxbuttons. OK, messageboxicon. information); return;} private void testsqlserverconn () {sqlconnection = new sqlconnection (); try {sqlconnection. connectionstring = connnectionstring; sqlconnection. open (); sqlconnection. close (); connsuccess = true;} catch {sqlconnection. close (); connsuccess = false ;}}

 

The code to prevent the animation window from being closed by Alt + F4 is as follows:

 
Public partial class formconnserver: FORM {public bool canclose = true; Public formconnserver () {initializecomponent (); formclosing + = new callback (formconnserver_formclosing);} private void handle (Object sender, formclosingeventargs e) {If (! Canclose) E. Cancel = true ;}}

AllSource codePackage download: http://files.cnblogs.com/BudEasyCode/ConnDB.rar

The code is very simple. You can understand it at a Glance. It can also be used to connect networks, complex time-consuming computing, and other application scenarios. About EASYCODE.. Net code generator 2.20 upgraded version, we are also working on the development and testing process, you can view the Code Generator details through my blog, or from our official website: the http://www.budeasycode.com to view.

 

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.