Asp.net accee database connection instability Solution

Source: Internet
Author: User

The error message is as follows:
Write the following method to return the number of rows affected by the data operation: CopyCode The Code is as follows: Private int getreturnvalue (string SSTR, string conn ){
Oledbconnection odbconn = accesshelp (conn );
Oledbcommand odbcmd = new oledbcommand (SSTR, odbconn );
Return odbcmd. executenonquery ();
}

Use the following method to call this class:Copy codeThe Code is as follows: public int wsbm (string [] Str ){
Stringbuilder sb = new stringbuilder ();
SB. append ("insert into"). append ("wsbm (ZY, studentname, parentname )");
SB. append ("values (");
SB. append ("'" + STR [0] + "', '" + STR [1] + "', '" + STR [2] + "'");
SB. append (")");
Return getreturnvalue (sb. tostring (), "odbconn ");
}

Send data in this event:Copy codeThe Code is as follows: protected void bttj_click (Object sender, eventargs e ){
String [] SSTR = new string [] {
This. ddlzy. selecteditem. Text,
This. tbname. Text,
This. tbbb. Text,
};
If (Ad. wsbm (SSTR)> 0 ){
Response. Write ("<SCRIPT> alert ('inserted successfully! ') </SCRIPT> ");
}
}

The execution result is as follows. Why ???

The connection method is as follows:Copy code The Code is as follows: Private oledbconnection accesshelp (string Str ){
Oledbconnection odbconn = new oledbconnection ();
Try {
String SSTR2 = "provider = Microsoft. jet. oledb.4.0; persist Security info = false; Data Source = "+ server. mappath (@ "app_data \ # fdaeg35 @ # GDS. mdb ");
Odbconn. connectionstring = SSTR2;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
Else {
SSTR2 = "DBQ =" + server. mappath (@ "app_data \ # fdaeg35 @ # GDS. mdb ") +"; defaultdir =; driver = {Microsoft Access Driver (*. MDB )};";
Odbconn. connectionstring = SSTR2;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
}
}
Catch {
Try {
// Odbconn = new oledbconnection (configurationmanager. connectionstrings [STR]. connectionstring );
Odbconn. connectionstring = configurationmanager. connectionstrings [STR]. connectionstring;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
}
Catch {
String sstr1 = @ "provider = Microsoft. Jet. oledb.4.0; persist Security info = false; Data Source =. \ app_data \ # fdaeg35 @ # GDS. mdb ";
// Odbconn = new oledbconnection (sstr1 );
Odbconn. connectionstring = sstr1;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
}
}
Return odbconn;
}

I tested all the link Methods locally in the previous article, and there was no problem at all. It was absolutely no problem in terms of performance, it was okay locally when I finished the development. When I got to the domain name space, there was a problem. Here I checked my exception problem: the exception result.

The request times out and the connection pool has reached the upper limit. After reading this section, I thought about it. My connection was already set to 15 seconds by default by the system. Then I changed the connection time to 1 minute. Such as code:

Copy code The Code is as follows: Private oledbconnection accesshelp (string Str ){
Oledbconnection odbconn = new oledbconnection ();
Try {
String SSTR2 = "provider = Microsoft. jet. oledb.4.0; persist Security info = false; connection timeout = 120; Data Source = "+ server. mappath (@ "app_data \ # fdaeg35 @ # GDS. mdb ");
Odbconn. connectionstring = SSTR2;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
Else {
SSTR2 = "DBQ =" + server. mappath (@ "app_data \ # fdaeg35 @ # GDS. mdb ") +"; defaultdir =; driver = {Microsoft Access Driver (*. MDB)}; connection timeout = 120 ;";
Odbconn. connectionstring = SSTR2;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
}
}
Catch {
Try {
// Odbconn = new oledbconnection (configurationmanager. connectionstrings [STR]. connectionstring );
Odbconn. connectionstring = configurationmanager. connectionstrings [STR]. connectionstring;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
}
Catch {
String sstr1 = @ "provider = Microsoft. Jet. oledb.4.0; persist Security info = false; connection timeout = 120; Data Source =. \ app_data \ # fdaeg35 @ # GDS. mdb ";
// Odbconn = new oledbconnection (sstr1 );
Odbconn. connectionstring = sstr1;
If (odbconn. State = connectionstate. Closed ){
Odbconn. open ();
}
}
}
Return odbconn;
}

As a result, I changed the code to solve the connection timeout problem.
Written in the previous articles to connect to the database, the default time for changing the connection pool is longer because of instability. After the change, if the refresh is not stopped during a very slow time, however, there was a serious problem: the ordinary data loss problem came out. I used the test software to test the problem. I didn't see the result because our local girl had a way to test it, my configuration is a little different from the server configuration. As mentioned above, we usually use a website instead of a local server, my class is to change the connection method of sqlhelper to the data sqldatabase to the Access database, and the class will eliminate the problem. I just drop his class directly, some parameters are given, so there is no problem with these parameters. It can be displayed in a very slow time. If you look at them, you will not see the result, I later saw in my blog that I did not have a slight attribute. This attribute is: the connection error concurrency time under the oldbconnection member connectiontimeout, if you are stuck, it is defined as the timeout state by default for 30 seconds. Repair the file and restore it to normal. As a result, I gave this attribute a minute and it would be normal. I suggest you do not give it too long. It takes a long time to give it. If something really goes wrong, you have to pin your computer into the screen. Always try this connection there. Then fewer and fewer people will access the website. Copy code The Code is as follows: Private Static void preparecommand (oldbcommand command, oldbconnection connection, oldbtransaction transaction, commandtype, string commandtext, sqlparameter [] commandparameters, out bool mustcloseconnection ){
If (command = NULL) throw new argumentnullexception ("command ");
If (commandtext = NULL | commandtext. Length = 0) throw new argumentnullexception ("commandtext ");
// If the provided connection is not open, we will open it
If (connection. State! = Connectionstate. Open ){
Mustcloseconnection = true;
Connection. open ();
}
Else {
Mustcloseconnection = false;
}
// Associate the connection with the command
Command. Connection = connection;
// Set the command text (stored procedure name or SQL statement)
Command. commandtext = commandtext;
// Set the command time
Command. commandtimeout = 60;
// If we were provided a transaction, assign it
If (transaction! = NULL ){
If (transaction. Connection = NULL) throw new argumentexception ("the transaction was rollbacked or commited, please provide an open transaction.", "transaction ");
Command. Transaction = transaction;
}
// Set the command type
Command. commandtype = commandtype;
// Attach the command parameters if they are provided
If (commandparameters! = NULL ){
Attachparameters (command, commandparameters );
}
Return;
}

The above is the class I used, the modified time position. It has been marked with a background.
Well, this connection is not stable.
This article specializes in ASP. NET development. Thank you for your attention to my blog.
If you have any questions, you can add Q to me.

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.