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;
}
我的上片中把鏈接的方式全部在本地測試了下,一點(diǎn)問題也沒有,性能方面一樣是絕對沒問題的,他是在我開發(fā)完這個的時候在本地是好的,一發(fā)到域名空間去就有問題來了,這里我看了下我的異常問題:異常結(jié)果如圖結(jié)果把英文給翻譯過來說的是:請求超時,連接池已經(jīng)達(dá)到了最高上限制??赐旰笮亩鄾隽艘还?jié),回想了下,原來我的連接已經(jīng)被系統(tǒng)給自己默認(rèn)了15秒請求時間,后來我直接把連接時間改成了1分鐘。如代碼:
代碼如下:
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;
}
結(jié)果我把這些代碼改成了這樣了,就解決了連接超時的問題了。
在上面的幾篇中寫到連接數(shù)據(jù)庫,出現(xiàn)不穩(wěn)定是連接池的默認(rèn)時間改長些,改了之后,要是在很卡的時間內(nèi),不停的刷新,可是,就出來了有一個嚴(yán)重的問題是,平凡的數(shù)據(jù)丟失問題來了,結(jié)果我用測試軟件來測試,沒看到結(jié)果,因?yàn)槲覀儽镜孛糜修k法測試出來,我的配置和服務(wù)器的配置是有點(diǎn)不同的,在說了,通常以個服務(wù)器不是和我們的本地那樣,就我們一個網(wǎng)站在用,我的類是把SQLHelper連接數(shù)據(jù)Sql數(shù)據(jù)庫的連接方式改成了連接Access數(shù)據(jù)庫的,類就排除了問題,我只是直接掉用他的類,給了些參數(shù)而以,所以這些全是沒什么問題的,可以是在很卡的時間內(nèi)就會出現(xiàn),去望是找便了,就是沒看到結(jié)果,我就在后來在英文的博客中看到,原來我勿略了一個屬性,這個屬性是:OldbConnection成員下的ConnectionTimeout這個連接錯誤并發(fā)時間在什么時間內(nèi)結(jié)束,如果你是在很卡的情況下,正好就是被默認(rèn)的30秒個定義成看超時狀態(tài),你修下里面的如何文件就會恢復(fù)正常。結(jié)果我個這個屬性給了個1分鐘,就正常了。我建議你們別給得太長的時間了,給長時間了,一但真的出錯了,那可是要把別人的電腦卡定屏幕的哦。總是在那嘗試這連接。那經(jīng)后就會越來越少的人來訪問了。
代碼如下:
private static void PrepareCommand(OldbCommand command, OldbConnection connection, OldbTransaction transaction, CommandType 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;
}
上面是我用的那個類,修改的時間位置。已經(jīng)用背景標(biāo)出來了。
呵呵,這個連接不穩(wěn)定的問題到此就結(jié)束了。
本文專業(yè)技術(shù)是ASP.Net開發(fā),在次謝謝你對我的博客的關(guān)注。
有問題也可以加我Q我哦。聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com