时间:2021-07-01 10:21:17 帮助过:33人阅读
- <br>private OleDbConnection AccessHelp(string str){ <br>OleDbConnection odbconn = new OleDbConnection(); <br>try{ <br>string sStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb"); <br>odbconn.ConnectionString = sStr2; <br>if (odbconn.State == ConnectionState.Closed){ <br>odbconn.Open(); <br>} <br>else{ <br>sStr2 = "DBQ=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"; <br>odbconn.ConnectionString = sStr2; <br>if (odbconn.State == ConnectionState.Closed){ <br>odbconn.Open(); <br>} <br>} <br>} <br>catch{ <br>try{ <br>//odbconn = new OleDbConnection(ConfigurationManager.ConnectionStrings[str].ConnectionString); <br>odbconn.ConnectionString = ConfigurationManager.ConnectionStrings[str].ConnectionString; <br>if (odbconn.State == ConnectionState.Closed){ <br>odbconn.Open(); <br>} <br>} <br>catch{ <br>string sStr1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=.\App_Data\#fdaeg35@#gds.mdb"; <br>//odbconn = new OleDbConnection(sStr1); <br>odbconn.ConnectionString = sStr1; <br>if (odbconn.State == ConnectionState.Closed){ <br>odbconn.Open(); <br>} <br>} <br>} <br>return odbconn; <br>} <br> <br>我的上片中把链接的方式全部在本地测试了下,一点问题也没有,性能方面一样是绝对没问题的,他是在我开发完这个的时候在本地是好的,一发到域名空间去就有问题来了,这里我看了下我的异常问题:异常结果如图
- <p style="TEXT-ALIGN: center"><img height="173" alt="" src="https://img.gxlcms.com//Uploads-s/new/2019-09-19-201919/20090716011947292.jpg" width="373"></p>
- <p style="TEXT-ALIGN: left"> 结果把英文给翻译过来说的是:请求超时,连接池已经达到了最高上限制。看完后心多凉了一节,回想了下,原来我的连接已经被系统给自己默认了15秒请求时间,后来我直接把连接时间改成了1分钟。如代码:<br><span><u></u></span> 代码如下:</p><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>private OleDbConnection AccessHelp(string str) { <br>OleDbConnection odbconn = new OleDbConnection(); <br>try { <br>string sStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;connection timeout=120;Data Source=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb"); <br>odbconn.ConnectionString = sStr2; <br>if (odbconn.State == ConnectionState.Closed) { <br>odbconn.Open(); <br>} <br>else{ <br>sStr2 = "DBQ=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};connection timeout=120;"; <br>odbconn.ConnectionString = sStr2; <br>if (odbconn.State == ConnectionState.Closed) { <br>odbconn.Open(); <br>} <br>} <br>} <br>catch { <br>try { <br>//odbconn = new OleDbConnection(ConfigurationManager.ConnectionStrings[str].ConnectionString); <br>odbconn.ConnectionString = ConfigurationManager.ConnectionStrings[str].ConnectionString; <br>if (odbconn.State == ConnectionState.Closed) { <br>odbconn.Open(); <br>} <br>} <br>catch { <br>string sStr1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;connection timeout=120;Data Source=.\App_Data\#fdaeg35@#gds.mdb"; <br>//odbconn = new OleDbConnection(sStr1); <br>odbconn.ConnectionString = sStr1; <br>if (odbconn.State == ConnectionState.Closed) { <br>odbconn.Open(); <br>} <br>} <br>} <br>return odbconn; <br>} <br> <br>结果我把这些代码改成了这样了,就解决了连接超时的问题了。 <br>在上面的几篇中写到连接数据库,出现不稳定是连接池的默认时间改长些,改了之后,要是在很卡的时间内,不停的刷新,可是,就出来了有一个严重的问题是,平凡的数据丢失问题来了,结果我用测试软件来测试,没看到结果,因为我们本地妹有办法测试出来,我的配置和服务器的配置是有点不同的,在说了,通常以个服务器不是和我们的本地那样,就我们一个网站在用,我的类是把SQLHelper连接数据Sql数据库的连接方式改成了连接Access数据库的,类就排除了问题,我只是直接掉用他的类,给了些参数而以,所以这些全是没什么问题的,可以是在很卡的时间内就会出现,去望是找便了,就是没看到结果,我就在后来在英文的博客中看到,原来我勿略了一个属性,这个属性是:OldbConnection成员下的ConnectionTimeout这个连接错误并发时间在什么时间内结束,如果你是在很卡的情况下,正好就是被默认的30秒个定义成看超时状态,你修下里面的如何文件就会恢复正常。结果我个这个属性给了个1分钟,就正常了。我建议你们别给得太长的时间了,给长时间了,一但真的出错了,那可是要把别人的电脑卡定屏幕的哦。总是在那尝试这连接。那经后就会越来越少的人来访问了。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>private static void PrepareCommand(OldbCommand command, OldbConnection connection, OldbTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection) { <br>if (command == null) throw new ArgumentNullException("command"); <br>if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); <br>// If the provided connection is not open, we will open it <br>if (connection.State != ConnectionState.Open) { <br>mustCloseConnection = true; <br>connection.Open(); <br>} <br>else { <br>mustCloseConnection = false; <br>} <br>// Associate the connection with the command <br>command.Connection = connection; <br>// Set the command text (stored procedure name or SQL statement) <br>command.CommandText = commandText; <br>//Set the command Time <br>command.CommandTimeout = 60; <br>// If we were provided a transaction, assign it <br>if (transaction != null) { <br>if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); <br>command.Transaction = transaction; <br>} <br>// Set the command type <br>command.CommandType = commandType; <br>// Attach the command parameters if they are provided <br>if (commandParameters != null) { <br>AttachParameters(command, commandParameters); <br>} <br>return; <br>} <br> <br>上面是我用的那个类,修改的时间位置。已经用背景标出来了。 <br>呵呵,这个连接不稳定的问题到此就结束了。 <br>本文专业技术是ASP.Net开发,在次谢谢你对我的博客的关注。 <br>有问题也可以加我Q我哦。 <p></p></li><li> </li><li> </li></ol></pre></li></ol></pre>