在ASP.NET 2.0中,提供了更方便的配置文件访问的类,具体可以到 System.Configuration 名称空间下进行查看。提供一种在开发过程中常用的得到数据库字符串的方法.这样在网站发布时不需要修改相关的数据连接语句就可以直接使用了。
Private cmd As IDbCommand = New SqlCommand()
Private strConnectionString As String = ""
Dim objConnectionStringSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("connectionstring")
strConnectionString = objConnectionStringSettings.ConnectionString
Dim cnn As New SqlConnection()
cnn.ConnectionString = strConnectionString
cmd.Connection = cnn
cmd.CommandType = CommandType.StoredProcedure
在web.config中相应的存在:
<connectionStrings>
<add name="connectionstring" connectionString="Persist Security Info=False;User ID=sa;Password=;Initial Catalog=DataBase1;Server=(local);" providerName="System.Data.SqlClient"/>
</connectionStrings>