asp-classic – 从ASP.Classic中的Web.Config读取ConnectionString
发布时间:2020-05-23 04:51:45 所属栏目:asp.Net 来源:互联网
导读:我有一个ASP文件.其实我连接到DataBase与我的文件中的一个connectionString. sConnString = Driver={SQL Server}; Server=localhost; Database=DB 有没有办法从Web.Config中读取ConnectionString? 编辑: 得到它适用于: Imports a connection string fro
|
我有一个ASP文件.其实我连接到DataBase与我的文件中的一个connectionString. 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
谢谢您的帮助 解决方法由于Web.Config文件是XML,所以只需将其加载到 XML DOM中即可访问其元素.(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api2 – Web Api 2 Post – UrlHelper.Link不能
- asp.net读取excel文件的三种方法示例
- asp-classic – VBScript Out of Memory Error
- ASP.Net将用户数据存储在验证Cookie中
- Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附
- asp.net-mvc-4 – 使用WebApi和外部登录的ASP.NET身份
- asp.net-mvc – 如何在Ajax窗体的asp.net mvc中提交下拉列表
- asp.net-mvc – ASP.NET MVC的初学者在线资源是什么?
- asp.net – 如何在运行时设置ObjectDataSource选择参数
- asp.net-mvc – 在ASP.NET MVC 3中授权当前用户对控制器和操
推荐文章
站长推荐
- asp.net-mvc – ASP.NET MVC/C++#:可以使用Html
- asp.net-mvc-3 – MVC3未终止字符串常量语法错误
- asp.net – 谷歌浏览器在浏览器关闭后保留会话变
- asp.net-mvc-4 – MVC4和MVCContrib
- asp.net-mvc – 带vNext的MVC 6:我们还需要Glob
- asp.net-mvc – 用于OpenID OAuth简单身份验证的
- asp.net-web-api – 什么时候使用HttpMessageHan
- asp.net-mvc – 在Owin Identity和Asp.Net MVC中
- asp.net-mvc-3 – 比较(密码)属性
- 这两种方法有什么区别?
热点阅读
