2015-06-16 141 views
1

我已經給出了一個用傳統ASP編寫的Web應用程序,用於從Windows 2003 Server(SQL Server 2000和IIS 6)移植到Windows 2008 Server(SQL Server 2008和IIS 7.5) 。經典ASP global.asa SQL Server 2008連接字符串

該站點使用GLOBAL.ASA文件來定義全局變量,其中之一是連接字符串(cnn)以連接到SQL Server。

下面是從GLOBAL.ASA(舊)的連接字符串:

Sub Application_OnStart 
    Dim cnnDem, cnnString 
    Set cnnDem = Server.CreateObject("ADODB.Connection") 
    cnnDem.CommandTimeout = 60 
    cnnDem.Mode = admodeshareexclusive 
    cnnString = "Provider=SQLOLEDB; Data Source=192.xxx.x.xx; User Id=xxxx; Password=xxxxx; default catalog=xxxxxxx;" 
    Application("conString")=cnnString 
    Call cnnDem.Open(cnnString) 
    Application("cnn") = cnnDem 
End Sub 

.ASP頁面然後使用cnn值是這樣的:

strSQL = "Select * From tblUtilities order by companyname" 
Set rs = Server.CreateObject("ADODB.Recordset") 
rs.Open strSQL, Application("cnn"), adOpenKeyset 

但是我不能讓連接字符串連接 - 我削減了一個「無法登錄」的錯誤消息(無論我嘗試了什麼登錄ID)。

我編輯了GLOBAL.ASA文件如下,它的工作原理。

Sub Application_OnStart 
    Dim cnnDem, cnnString 
    Set cnnDem = Server.CreateObject("ADODB.Connection") 
    cnnDem.CommandTimeout = 60 
    cnnString = "Provider=SQLNCLI10.1;User Id=xxxx; Password=xxxxx;Initial Catalog=xxxxxxx;Data Source=xxxxxx\SQLEXPRESS;" 
    Application("conString")=cnnString 
    Application("cnn")=cnnString 
    Call cnnDem.Open(cnnString) 
End Sub 

的主要區別是,cnn現在包含連接字符串,其中如先前cnn是指ADOBD.Connection的對象。

我的問題是這會對應用程序產生什麼影響(如果有的話)。我已經完成了一些基本的(本地)測試,現在一切都看起來不錯。但是我想知道是否可能會有多用戶問題(或者這種性質的問題),當這個站點再次部署時可能會出現這種問題。

+4

從MSDN - [不緩存在'應用程序的連接(或會話)'](https://msdn.microsoft.com/en-us/library/bb727078.aspx#EXAA) - 無論如何,在連接池的情況下,幾乎沒有任何好處。您可以將連接字符串本身存儲在應用程序狀態中。 – StuartLC

+0

謝謝你,斯圖爾特 –

回答

0

我將連接字符串保存在Global.asa中,但是在根據需要加載的單獨函數中創建連接。應用程序連接對象可能不知道可能會關閉該連接的臨時網絡問題,然後未來嘗試使用該連接將不會成功。

希望這是有道理的。

0

一個連接創建一個數據庫連接字符串的最好和最簡單的方法是摺痕的根目錄或其他地方一個新的ASP文件,並在連接字符串進去:

//Global.asp//

<% 
Dim connectionString 
connectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=YourSQLServer;UID=sa;PWD=*******;DATABASE=YourDataBase" 
%> 

然後在每個文件中創建一個包含您要調用此連接的語句。

<!-- #include virtual="global.asp" --> 

然後,你需要設置你的連接呼叫,只需使用你的代碼連接到數據庫:

<% 
Set adoCon = Server.CreateObject("ADODB.Connection") 
adoCon.Open = ConnectionString 
Set rsReports = Server.CreateObject("ADODB.Recordset") 
strSQL = "Select * From Customers" 
rsReports.Open strSQL, adoCon 
%>