Create a connectiontestinfo class
Using system. Data. sqlclient;
Using system. Data;
Public class connectiontestinfo
{
Private Static sqlconnection mysqlconnection; // mysqlconnection is a sqlconnection object
Private Static string connectionstring = "";
Private Static bool iscanconnectioned = false;
/// <Summary>
/// Test whether the database connection is successful
/// </Summary>
/// <Returns> </returns>
Public static bool connectiontest ()
{
// Obtain the database connection string
Connectionstring = connectioninfo. connectionstring ();
// Create a connection object
Mysqlconnection = new sqlconnection (connectionstring );
// Connectiontimeout can be set as a read-only attribute after. NET 2.0 in. Net 1.x. You need to set it in the connection string.
// For example, Server =.; uid = sa; Pwd =; database = PMIS; Integrated Security = sspi; connection timeout = 30
// Mysqlconnection. connectiontimeout = 1; // set the connection timeout time
Try
{
// Open Database
// Open the database
Mysqlconnection. open ();
Iscanconnectioned = true;
}
Catch
{
// Can not open database
// Connection fails if the connection fails.
Iscanconnectioned = false;
}
Finally
{
// Close the database
// Close the database connection
Mysqlconnection. Close ();
}
// Mysqlconnection is a sqlconnection object
If (mysqlconnection. State = connectionstate. Closed | mysqlconnection. State = connectionstate. Broken)
{
// Connection is not available
Return iscanconnectioned;
}
Else
{
// Connection is available
Return iscanconnectioned;
}
}
}
The database string calls the connectioninfo-like method connectionstring
Public class connectioninfo
{
Public connectioninfo (){}
/// <Summary>
/// Read the database connection string from the configuration file
/// </Summary>
/// <Returns> </returns>
Public static string connectionstring ()
{
Return (configurationsettings. etettings ["connectionstring"]);
}
}
Resolving ASP. NET web applicatio timeout has reached. The timeout has expired before the operation is completed or the server has not responded
"The timeout has reached. The time-out period has expired or the server has not responded before the operation is completed"
The initial analysis reason is that the connection timed out during the MSSQL operation. I did not pay attention to this issue before, probably because I set the connection time limit in the configuration file and found a solution online, most of them are solved in database connection strings.
Sqlconnection con = New Sqlconnection ( " Server =.; database = mydb; uid = sa; Pwd = password; " ) Changed:
Sqlconnection con = New Sqlconnection ( " Server =.; database = mydb; uid = sa; Pwd = password; Connect timeout = 500 " ) It seems ineffective. If it is still running for 30 seconds, a timeout is reported!
Suddenly it seems that you can connect to the database Code Indicates that the con attribute has a connectiontimeout,
Sqlconnection con = New Sqlconnection ( " Server =.; database = mydb; uid = sa; Pwd =; " );
Con. connectiontimeout = 180 ; // Error. The connectiontimeout attribute is read-only! The attempt failed, and then looked at the command object property, and found that it also has a similar property! Commandtimeout settings:
Sqlcommand cmd = New Sqlcommand ();
Cmd. commandtimeout = 180 ; Run again to solve the problem. The time set here is 180 seconds, that is, three minutes! You can set it as needed. If it is too long, you can also set it to 0. When this attribute is set to 0, there is no time limit. This attribute value should be used with caution. You also need to set the time limit for running HTTP requests in the web. config configuration file.
< System . Web >
< Httpruntime Maxrequestlength = "102400" Executiontimeout = "720" />
</ System. Web >