Asp classic 从ASP.Classic中的Web.Config读取ConnectionString
我有一个ASP文件。实际上,我使用文件中的connectionString连接到数据库Asp classic 从ASP.Classic中的Web.Config读取ConnectionString,asp-classic,vbscript,Asp Classic,Vbscript,我有一个ASP文件。实际上,我使用文件中的connectionString连接到数据库 sConnString = "Driver={SQL Server}; Server=localhost; Database=DB" 有没有办法从Web.Config读取ConnectionString 编辑: 让它工作: ' Imports a connection string from an xml file (usually web.config) Function ImportConnection
sConnString = "Driver={SQL Server}; Server=localhost; Database=DB"
有没有办法从Web.Config读取ConnectionString
编辑:
让它工作:
' Imports a connection string from an xml file (usually web.config)
Function ImportConnectionString(webConfig, attrName, reformatDSN)
Dim oXML, oNode, oChild, oAttr, dsn
Set oXML=Server.CreateObject("Microsoft.XMLDOM")
oXML.Async = "false"
oXML.Load(Server.MapPath(webConfig))
Set oNode = oXML.GetElementsByTagName("connectionStrings").Item(0)
Set oChild = oNode.GetElementsByTagName("add")
' Get the first match
For Each oAttr in oChild
If oAttr.getAttribute("name") = attrName then
dsn = oAttr.getAttribute("connectionString")
If reformatDSN Then
' Optionally reformat the connection string (adjust as needed)
dsn = Replace(dsn, "User ID=", "UID=")
dsn = Replace(dsn, "Password=", "PWD=")
dsn = Replace(dsn, "Data Source=", "Server=")
dsn = Replace(dsn, "Initial Catalog=", "Database=")
dsn = Replace(dsn, "Persist Security Info=True;", "")
dsn = "Provider=MSDASQL;Driver={SQL Server};" & dsn
End If
ImportConnectionString = dsn
Exit Function
End If
Next
End Function
用法:
dsn = ImportConnectionString("..\web.config", "ConnectionStringName", false)
sql = "SELECT * FROM MyTable"
Set oConn = Server.CreateObject("ADODB.Connection")
Set oRS = Server.CreateObject("ADODB.RecordSet")
oConn.Open dsn
oRS.Open sql, oConn
If NOT oRS.EOF Then
oRS.MoveFirst
Do
Response.Write(" " & oRS("Column1") & "<br/>")
oRS.MoveNext
Loop Until oRS.EOF
End If
dsn=ImportConnectionString(“..\web.config”,“ConnectionStringName”,false)
sql=“从MyTable中选择*
设置oConn=Server.CreateObject(“ADODB.Connection”)
Set oRS=Server.CreateObject(“ADODB.RecordSet”)
开放dsn
开放式sql
如果不是oRS.EOF,则
先走一步
做
回答。写(“&OR(“Column1”)和“
”)
下一个
循环直到oRS.EOF
如果结束
感谢您的帮助因为Web.Config文件是XML文件,所以只需将其加载到中并以这种方式访问其元素。感谢帮助我的链接alotI还找到了显示如何从Web提取连接字符串的链接。config@vanhelgen你链接的页面正是webaware在回答中描述的。