首先在你的config.web 文件中创建以下参数:
<appsettings>
<add key="gConn" value="server=local;uid=sa;pwd=secret;database=pubs" />
</appsettings>
接着建立dbConn.vb 文件。
Imports System
Imports System.Web
Imports System.Collections
Namespace WebDB
Public Class WebDBconn
Shared m_ConnectionString As String
Shared ReadOnly Property ConnectionString As String
Get
If m_ConnectionString = "" Then
Dim appsetting As Hashtable = CType(HttpContext.Current.GetConfig("appsettings"), Hashtable)
'使用 config.web 中设立好的连接字符串
m_ConnectionString = CStr(appsetting("DBConnString"))
If m_ConnectionString = "" Then
throw new Exception("Dtabase Connection Value not set in Config.web")
End if
End If
' 返回连接字符串
return m_connectionString
End Get
End Property
End Class
End Namespace
'下面编译dll文件:创建一个批处理文件, 命名为 MakeDll.bat ,存放在dbConn.vb 相同的目录,其中的内容如下
set odir=dbConn.dll
set assemblies=System.Web.dll
vbc /t:library /out:%odir% /r:%assemblies% dbConn.vb
执行批处理文件, 将 dbconn.dll 拷贝到你的WEB 目录的 BIN 目录下并创建如下的.aspx 文件:
<%@ Page Description="ASP+ document" EnableSessionState="false" MaintainState="false" %>
<%@ Import Namespace="WebDB" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
response.write(WebDBconn.ConnectionString)
End Sub
</script>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>