asp-classic – 从ASP.Classic中的Web.Config读取ConnectionStri
发布时间:2020-12-15 19:25:45 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP文件.其实我连接到DataBase与我的文件中的一个connectionString. sConnString = "Driver={SQL Server}; Server=localhost; Database=DB" 有没有办法从Web.Config中读取ConnectionString? 编辑: 得到它适用于: ' Imports a connection string f
我有一个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.config
- asp.net-mvc-4 – 下划线字符串模型绑定器
- asp.net-mvc-3 – 如何在MVC应用程序中使用POCO
- asp.net-mvc – Asp.Net Mvc无法注销
- asp.net-mvc – 可以在桌面应用程序开发中使用MVC设计模式/
- asp.net-mvc – .Net Web API抛出异常/返回响应/返回错误响
- asp.net-mvc – 在EF 6中设置命令超时
- asp.net – 如何根据属性值更新设计时UserControl接口?
- asp.net-mvc – 如何调试Minification失败 返回未最终内容
- 如何在ASP.NET WebService调用中动态初始化文化?
推荐文章
站长推荐
- 为什么asp.net将页面包装在一个表单中?
- 什么可能导致“客户端断开连接”的ASP.NET异常?
- asp.net-mvc – 如何在ASP.NET MVC中执行辅助操作
- asp.net – .NET VirtualPathProviders和预编译
- asp.net – Webservices可以作为单身人士引起不同
- ASP.NET:隐藏gridview中的列
- asp.net – 在IIS上部署MVC应用程序时,我收到一个
- asp.net-identity – 保护整个ASP.NET 5 MVC 6应
- 实体框架 – 使用实体框架数据模型添加验证属性
- asp.net – 什么是Thread.CurrentPrincipal,它有
热点阅读